Sinch委托方法“shouldSendPushNotification”未被调用

时间:2014-07-31 19:00:53

标签: ios objective-c sinch

我正在和Sinch一起玩,并且在推送通知方面遇到了一些问题。

  • 首先,我可以使用Sinch发送和接收消息(两个设备 有两个不同的Sinch ID)。这意味着Sinch客户端 正确配置。

  • 其次,我可以确认推送通知已正确设置 这两个设备,因为我可以发送推送通知给他们 Parse.com。它们都有有效的推送通知令牌。

然后在我的应用程序中,我发现Sinch委托方法:shouldSendPushNotification在接收方不是"在线"时没有被调用。

我搜索了SO并发现了一个类似的问题(Sinch, message shouldSendPushNotification not being called)建议检查messageSent回拨。

因此我在接收方尝试了以下内容:

  • 按回家
  • 将应用程序置于后台
  • 强制退出应用程序(双击主页,从后台删除应用程序)
  • 启用航班模式

在发送消息之后,我可以看到:

- (void)messageSent:(id<SINMessage>)message recipientId:(NSString *)recipientId

正在发件人方面调用

recipientId与目标设备相同。但是,正如Sinch的文档中所述,shouldSendPushNotification方法永远不会被调用。

由于未调用此shouldSendPushNotification方法,因此不会向目标设备发送任何推送通知。

我已经在这个问题上工作了好几天,而且非常渴望了解解决方案,我们非常感谢您的帮助。

测试环境

iOS 8 beta 4中的两个设备和iOS 7.1.2中的一个设备

使用XCode 6 beta 4构建

代码:

AppDelegate.m

中设置Sinch消息服务
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

    // setup Parse

    [Parse setApplicationId:@"xxxxx"
                  clientKey:@"xxxxx"];
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // use registerUserNotificationSettings
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories: UIUserNotificationActionContextDefault]];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        // use registerForRemoteNotifications
        // Let the device know we want to receive push notifications
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    #else
    // use registerForRemoteNotifications
    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    #endif

    // app response from the notifications while in background
    NSDictionary* remotePush = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (remotePush) {
        // Extract the Sinch-specific payload from the Apple Remote Push Notification
        NSString* payload = [remotePush objectForKey:@"SIN"];
        // Get previously initiated Sinch client
        id<SINNotificationResult> result = [_client relayRemotePushNotificationPayload:payload];
        if (result.isMessage) {
            // Present alert notifying
            NSString *messageId = [[result messageResult] messageId];
            NSLog(@"Received messageid: %@", messageId);
        } else if (!result.isValid) {
            // Handle error
        }
        NSLog(@"Received Payload: %@", payload);
    }

    return YES;
}


- (void)initSinchClientWithUserId:(NSString *)userId {
    if (!_client) {
        _client = [Sinch clientWithApplicationKey:@"xxxx"
                                applicationSecret:@"xxxx"
                                  environmentHost:@"sandbox.sinch.com"
                                           userId:userId];

        _client.delegate = self;

        [_client setSupportMessaging:YES];
        [_client setSupportPushNotifications:YES];
        [_client setSupportActiveConnectionInBackground:NO];
        [_client start];
        [_client startListeningOnActiveConnection];
    }
}

当应用程序启动时,此行会按预期调用

- (void)clientDidStart:(id<SINClient>)client {
    NSLog(@"Sinch client started successfully (version: %@)", [Sinch version]);
}

在应用程序的MessageSendingViewController内部

- (id<SINClient>)client {
    return [(AppDelegate *)[[UIApplication sharedApplication] delegate] client];
}

-(void)viewDidLoad {
...
[self.client messageClient].delegate = self;
...
}

2 个答案:

答案 0 :(得分:1)

您是否通过-[SINClient registerPushNotificationData:]方法注册“推送数据”(例如您的APN令牌)?

尝试类似:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [_client registerPushNotificationData:deviceToken];
}

http://www.sinch.com/docs/ios/user-guide/#pushnotifications

还有更多细节

答案 1 :(得分:0)

在Sinch的帮助下解决了问题。

首先确保在调用委托方法clientregisterPushNotificationData:deviceToken不是nil(0x0)。

在我的情况下,我需要在启动Sinch客户端后再次手动注册通知设置。

启动客户端并注册通知设置后,应该毫无问题地调用shouldSendPushNotification方法。