IOS Sinch当应用程序使用APN离线时调用(获取以前启动的客户端)

时间:2014-09-02 06:05:16

标签: ios sinch

我已经成功集成了Sinch SDK,并且在应用程序打开时工作效果很好,现在我正在处理我的应用程序离线时的通话。

使用以下方法,我可以看到推对,我将此推送对发送到服务器以进行推送。

- (void)call:(id<SINCall>)call shouldSendPushNotifications:(NSArray *) pushPairs {

}

在Appdelegate.m didFinishLaunchingWithOptions()方法中,我正在获得sinch特定的有效载荷。

 NSDictionary* remotePush = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    NSLog(@"%@",remotePush);
    if (remotePush) {
        NSLog(@"Entery ****");
        // Extract the Sinch-specific payload from the Apple Remote Push Notification
        NSString* payload = [[remotePush valueForKey:@"aps"] valueForKey:@"SIN"];

    //    [[userInfo valueForKey:@"aps"] valueForKey:@"content-available"];
        NSLog(@"Payload :%@",payload);
        // Get previously initiated Sinch client
        id<SINClient> client = _client;
        NSLog(@"Client ID %@",[client userId]);
        id<SINNotificationResult> result = [client relayRemotePushNotificationPayload:payload];
        NSLog(@"Result :%@",result);
        if (result.isCall && result.callResult.isTimedOut) {
            // Present alert notifying about missed call
        } else if (!result.isValid) {
            // Handle error
        }
    }

以下是完整的推送通知数据:

{
        aps =     {
            SIN = "AgEBdeibxmJBSK2Y7Nh/fz50VMhVBVQMKzkxODg0NzE4MjQx";
            alert = "";
            "content-available" = 1;
            type = 2;
        };
    }

来到这段代码:

// Get previously initiated Sinch client
    id<SINClient> client = _client;
    NSLog(@"Client ID %@",[client userId]);

当我打印客户端ID时,它是Nil,我需要如何获得以前发起的Sinch客户端?自App关闭后,客户端实例将被释放。

1 个答案:

答案 0 :(得分:4)

您需要自己保留userId,然后在重新启动应用时,使用您在最后一个应用关闭时保留的userId初始化SinchClient

最简单的可能是使用NSUserDefaults并保存字段&#34; userId&#34;您在用户登录时编写的内容,如果用户退出您的应用程序,则会删除

例如,开始时:

[[NSUserDefaults standardUserDefaults] setObject:userId forKey:@"userId"];

然后当你的应用程序通过APN启动时:

NSString *persistedUserId = [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"];
if (persistedUserId != nil) {
  _client = [Sinch clientWithApplicationKey:APPLICATION_KEY
                          applicationSecret:APPLICATION_SECRET
                            environmentHost:ENVIRONMENT
                                     userId:persistedUserId];
}
相关问题