Different action when push is received while the app is opened, from when the app is closed and you click on the notification

Answered

Comments

2 comments

  • Avatar
    PVJP

    Looking for an answer I found a solution, the PushNotification class has a getLaunchNotification function, described below (or here):

    PushNotification.prototype.getLaunchNotification = function( callback )

    Returns push notification payload if the app was started in response to push notification or null otherwise.

    This works well, I sent push notifications with custom data, and when the app was opened manually the callback function would not get any payload, if opened by clicking on the notification it would be called with the custom data I sent.

    The problem is that once it was opened by the push, any new push notifications arrived will also send the payload to the callback function (because it was originally opened by a push). So in my example above if someone opened the app by clicking on a notification, and then started typing with a friend, as soon as the next notification arrived from another friend he would be redirected to the other chat.

    To fix that I am using a parameter called onStart that is included in the payload, it will only be set to true if the push received opened the application, instead of only checking if the app was originally opened by a push.

    This way I can only change the app state when onStart is equal to true. Here is the code sample:

    pushNotification.getLaunchNotification(function(payload) {
                if(payload && payload.onStart) { //if there is a payload the app was last opened by push
                    var rcvdState = payload.userdata.state || 'tab.chats';
                    var rcvdParams = payload.userdata.params || {};
                    $state.go(rcvdState,rcvdParams);
                    console.log("Opened by push, rcvdState: " + rcvdState + " rcvdParams: " + JSON.stringify(rcvdParams));
                } else {
                    console.log("App was already opened, you won't be redirected to chat.");
                }
            });
    

    Thanks

  • Avatar
    Erandaka Bandara

    I would like to know that how did you manage to open chat screen in another window when a push come to the mobile and user select on that push.

Please sign in to leave a comment.