我在玩 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
与目标设备相同。但该shouldSendPushNotification
方法从未像 Sinch 文档中所述的那样被调用。
由于shouldSendPushNotification
未调用此方法,因此不会向目标设备发送任何推送通知。
我已经研究这个问题好几天了,非常想知道解决方案,感谢任何帮助。
测试环境
iOS 8 beta 4 中的两台设备和 iOS 7.1.2 中的一台设备
使用 XCode 6 beta 4 构建
代码:
在中设置 Sinch 消息服务AppDelegate.m
- (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;
...
}