dumbfingers
dumbfingers

Reputation: 7089

Sinch delegate method "shouldSendPushNotification" not being called

I'm playing with Sinch and have some problems with Push notifications.

Then in my app, I found that the Sinch delegate method: shouldSendPushNotification is not called when the receiver side is not "online".

I did a search on SO and found there's a similar question (Sinch, message shouldSendPushNotification not being called) which suggested to check the messageSent call back.

Therefore I tried the following in the receiver side:

After that when a message sent, I can see:

- (void)messageSent:(id<SINMessage>)message recipientId:(NSString *)recipientId

is being invoked in the sender's side and the recipientId is the same as the destination device's. But the shouldSendPushNotification method is never being called as stated in Sinch's documentation.

Since this shouldSendPushNotification method is not invoked, there will not be any push notifications being sent out to the destination device.

I've been working on this problem for several days, and very keen to know the solution, any help is appreciated.

Test environment

two devices in iOS 8 beta 4 and one in iOS 7.1.2

build using XCode 6 beta 4

Code:

Setting up the Sinch messaging service in 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];
    }
}

And this line is called as expected when the app starts

- (void)clientDidStart:(id<SINClient>)client {
    NSLog(@"Sinch client started successfully (version: %@)", [Sinch version]);
}

Inside the app's MessageSendingViewController

- (id<SINClient>)client {
    return [(AppDelegate *)[[UIApplication sharedApplication] delegate] client];
}

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

Upvotes: 0

Views: 940

Answers (2)

dumbfingers
dumbfingers

Reputation: 7089

Problem solved with some help from the Sinch.

First make sure that client is not nil (0x0) when the delegate method registerPushNotificationData:deviceToken is being called.

In my case, I need to manually register notification settings again after starting the Sinch client.

Once the client is started and notification settings are registered, the shouldSendPushNotification method should be called without any problems.

Upvotes: 0

cahlbin
cahlbin

Reputation: 1069

Are you registering "push data" (e.g. your APN token) via the method -[SINClient registerPushNotificationData:]?

Try something like:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [_client registerPushNotificationData:deviceToken];
}

(There is also more details here on http://www.sinch.com/docs/ios/user-guide/#pushnotifications)

Upvotes: 1

Related Questions