Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

PushPlugin.m init() should send pending notification when js side is ready #658

Closed
jimmy-tfp opened this issue Feb 29, 2016 · 5 comments
Closed

Comments

@jimmy-tfp
Copy link

On iOS:

Expected Behaviour

When your app is "not running" and you click a notification for that app, in PushPlugin.init() it finds "notificationMessage" and sends that to the app when the app is ready

Actual Behaviour

When your app is "not running" and you click a notification for that app, in PushPlugin.init() it finds "notificationMessage" and sends that to the app right away and the app has not had a chance to setup the rest of the calls needed to catch the notification.

Reproduce Scenario (including but not limited to)

  1. Kill your app
  2. Send it a notification
  3. Click on the notification
  4. Note it does not show up

Steps to Reproduce

see above

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iOS 9.2.1, 6 and 6p

Sample Push Data Payload

payload = json.dumps({
'aps': {
'alert': 'Push Test %d: %s' % (ident, str(uuid.uuid4())[:8]),
'badge': 4
}
})

Sample Code that illustrates the problem

here is my service initialize:

function initialize () {

        if (window.cordova) {

            $rootScope.$on('$cordovaPushV5:notificationReceived', function (event, notification) {

                console.log('$cordovaPushV5:notificationReceived');
                //alert('notificationReceived: ' + JSON.stringify(notification));

                if(notification.message) {

                }

                if(notification.additionalData && typeof notification.additionalData.foreground != 'undefined' && !notification.additionalData.foreground) {
                    $state.go('tab.inbox');
                }
            });


            var options = {
                android: {
                    senderID: ''
                },
                ios: {
                    alert: true,
                    badge: true,
                    sound: false,
                    clearBadge: true
                },
                windows: {}
            };

            $cordovaPushV5.initialize(options).then(function(deviceToken) {

                console.log('$cordovaPushV5:initialize');

                $cordovaPushV5.register().then(function(deviceToken) {

                    console.log('$cordovaPushV5:register');
                    console.log('deviceToken: ' + deviceToken);
                    peckService.saveDeviceToken($auth.getPayload().sub, deviceToken);

                    $cordovaPushV5.onError();
                    $cordovaPushV5.onNotification();

                    console.log('$cordovaPushV5:ready for notifications');

                }, function(err) {
                    console.log('register error: ' + err)
                });
            }, function(err) {
                console.log('initialize error: ' + err)
            });

        }
    }

Logs taken while reproducing problem

not sure how to get them...

The "Fix"

to "fix" this I put a delay before sending out the initial notification.

// if there is a pending startup notification, go ahead and process it after a delay to let the js warmup
if (notificationMessage) {
dispatch_async(dispatch_get_main_queue(), ^{
// it works everytime with 0.3 so 1.2 is just majik
[self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 1.2];
});
}

However it seems like the real fix should be to have a new method in the plugin called "IamFinallyReadyToReceiveThatOpeningNotificationNow" that would queue the notification when the js size is all ready.

@Fiveside
Copy link

I'm also experiencing this problem on android (version 5.0.2). Rolling back to phonegap-push-plugin version 1.3.0 fixes the issue. To the best of my knowledge, no intermediate versions fix the problem.

@macdonst macdonst added this to the Release 1.7.0 milestone Apr 11, 2016
@40x
Copy link

40x commented May 4, 2016

@jimmy-tfp can you show the native code for your temporary fix? Also where you added it (line number) ? This may help the non native folks out there

@jimmy-tfp
Copy link
Author

my hack is near line 245 of PushPlugin.m

Here is a larger cut of that file showing the lines above and below the hack:

[self setGcmSandbox:@NO];
if ([self usesGCM] &&
    (([gcmSandBoxArg isKindOfClass:[NSString class]] && [gcmSandBoxArg isEqualToString:@"true"]) ||
        [gcmSandBoxArg boolValue]))
{
    NSLog(@"Using GCM Sandbox");
    [self setGcmSandbox:@YES];
}

// if there is a pending startup notification, go ahead and process it after a delay to let the js warmup
if (notificationMessage) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // it works everytime with 0.3 so 1.2 is just majik
        [self performSelector:@selector(notificationReceived) withObject:nil afterDelay: 1.2];
    });
}

}];

}

  • (UIMutableUserNotificationAction *)createAction:(NSDictionary *)dictionary {

    UIMutableUserNotificationAction *myAction = [[UIMutableUserNotificationAction alloc] init];

    myAction = [[UIMutableUserNotificationAction alloc] init];
    myAction.identifier = [dictionary objectForKey:@"callback"];
    myAction.title = [dictionary objectForKey:@"title"];
    id mode =[dictionary objectForKey:@"foreground"];
    if (mode == nil || ([mode isKindOfClass:[NSString class]] && [mode isEqualToString:@"false"]) || ![mode boolValue]) {

@40x
Copy link

40x commented May 4, 2016

Thanks, just curious, if there is more than 1 unread notification in the tray, does it still pick just the one that was tapped on and alert/log it? Right now when I use alert. It seems like it is picking all of them and the latest one get alerted twice.

macdonst added a commit that referenced this issue Jun 6, 2016
macdonst added a commit that referenced this issue Jun 6, 2016
@macdonst macdonst removed the staging label Jun 6, 2016
@macdonst macdonst closed this as completed Jun 6, 2016
@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

4 participants