0

我已经成功集成了 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 关闭,客户端实例被释放。

4

1 回答 1

4

您需要自己保留 userId,然后在应用重新启动时,SinchClient使用您在上次应用关闭时保留的 userId 初始化 a。

最简单的可能是使用NSUserDefaults并保存用户登录时编写的字段“userId”,如果用户退出您的应用程序,则删除该字段。

例如,启动时:

[[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];
}
于 2014-09-02T06:54:29.140 回答