我在玩Sinch,在推送通知方面有一些问题。
然后,在我的应用程序中,我发现Sinch委托方法:当接收方不是“联机”时不调用shouldSendPushNotification。
我搜索了一下,发现有一个类似的问题(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;
...
}发布于 2014-08-07 14:25:01
在辛奇的帮助下解决了问题。
首先,确保在调用委托方法client时,registerPushNotificationData:deviceToken不是nil (0x0)。
在我的例子中,我需要在启动Sinch客户端之后再次手动注册通知设置。
一旦客户端启动并注册了通知设置,就应该没有任何问题地调用shouldSendPushNotification方法。
发布于 2014-08-05 11:29:00
您是否通过-[SINClient registerPushNotificationData:]方法注册"push data“(例如APN令牌)?
试一试如下:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[_client registerPushNotificationData:deviceToken];
}(这里还有关于http://www.sinch.com/docs/ios/user-guide/#pushnotifications的更多细节)
https://stackoverflow.com/questions/25066896
复制相似问题