【发布时间】:2014-07-31 19:00:52
【问题描述】:
我在玩 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 构建
代码:
在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;
...
}
【问题讨论】:
标签: ios objective-c sinch