我正在和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
- (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;
...
}
答案 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的帮助下解决了问题。
首先确保在调用委托方法client
时registerPushNotificationData:deviceToken
不是nil(0x0)。
在我的情况下,我需要在启动Sinch客户端后再次手动注册通知设置。
启动客户端并注册通知设置后,应该毫无问题地调用shouldSendPushNotification
方法。