首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sinch委托方法"shouldSendPushNotification“未被调用

Sinch委托方法"shouldSendPushNotification“未被调用
EN

Stack Overflow用户
提问于 2014-07-31 19:00:52
回答 2查看 937关注 0票数 0

我在玩Sinch,在推送通知方面有一些问题。

  • 首先,我可以使用Sinch发送和接收消息(两个设备具有两个不同的Sinch )。因此,这意味着Sinch客户端配置正确。
  • 其次,我可以确认在这两个设备上正确设置了推送通知,因为我可以在Parse.com上向它们发送推送通知。它们都有有效的推送通知令牌。

然后,在我的应用程序中,我发现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消息服务

代码语言:javascript
复制
- (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];
    }
}

当应用程序启动时,将按预期调用这一行。

代码语言:javascript
复制
- (void)clientDidStart:(id<SINClient>)client {
    NSLog(@"Sinch client started successfully (version: %@)", [Sinch version]);
}

在应用程序的MessageSendingViewController内部

代码语言:javascript
复制
- (id<SINClient>)client {
    return [(AppDelegate *)[[UIApplication sharedApplication] delegate] client];
}

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

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-07 14:25:01

在辛奇的帮助下解决了问题。

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

在我的例子中,我需要在启动Sinch客户端之后再次手动注册通知设置。

一旦客户端启动并注册了通知设置,就应该没有任何问题地调用shouldSendPushNotification方法。

票数 0
EN

Stack Overflow用户

发布于 2014-08-05 11:29:00

您是否通过-[SINClient registerPushNotificationData:]方法注册"push data“(例如APN令牌)?

试一试如下:

代码语言:javascript
复制
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [_client registerPushNotificationData:deviceToken];
}

(这里还有关于http://www.sinch.com/docs/ios/user-guide/#pushnotifications的更多细节)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25066896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档