0
1

Hello,

I am developing a messaging app using ionic/cordova for Android and iOS. So far it is working great with the push notifications (except when I used the manifest check button in the configuration page, it suggested several modifications to the manifest that were causing the app to crash when it got a notification, turns out that the manifest after the plugin installation is good enough, maybe the manifest check tool needs an update :D).

What I want to do is have a have a different behaviour in these two situations:

1) The app is opened and the user gets a push, nothing happens in the UI and the custom data is handled in the background, the user will just see that a notification arrived in the notification bar up top. e.g. User is typing a message to his friend and a message from another friend arrives, the push is receive to notify the user but he stays on the same chat session.

2) The app is closed or in the background and the user gets a push, when he clicks on it the user is automatically taken to the app, but now the user is taken to the chat session the message came from. e.g. User is not on the app, then a message from a friend arrives, when he clicks on it he is taken to the chat session that message is from.

So in short, I don't want the user to be taken to the other chat screen when he is on the app. I know how to take the user to the other screen, but don't know how to differentiate the two situations inside the function that handles the push-notification event.

If you guys could help me finding a way to do this, I would be extremelly grateful :D

Regards, Paulo

asked 26 Aug '15, 00:05
paulovitorjp's gravatar image

paulovitorjp
1113

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.

(25 Jun '16, 16:58) erandakaanupama erandakaanupama's gravatar image

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

permanent link
answered 28 Aug '15, 04:23
paulovitorjp's gravatar image

paulovitorjp
1113

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×190
×141
×39
×11
×6

question asked: 26 Aug '15, 00:05

question was seen: 7,799 times

last updated: 25 Jun '16, 16:59