This is the first time i use notification system to run a function to refresh data (i was using realtime data with firebase ) so i dont entirely understand how it works.
what i want to do is separate notification by payload name and run function regarding payload of the notification arriving.
if i send a message , the payload set will be message , while receiving the notification i want to be able to just run the message function to refresh the data and follow the same pattern with all others function
Hey Kobo,
I believe it would help you to understand that you have full control over what you put inside the push notification payload. It’s not really restricted to something predefined.
So, for example, one way to handle what you want to achieve would be to send a push notification with a payload like this:
What happens with this when the phone receives the notification is - the title and body params under alert are used by the phone OS to display the notification, hence you should always send these when you want to deliver a visible notification.
Now, when notification is delivered to your push.onReceivedMessage function, you get the whole object, so you can start acting on what’s inside of that data element.
Note that this is also a way for you to deliver a “silent” notification, if you omit the alert element. So, theoretically, you should be able to do this:
{
"aps":{
"data":{
"type":"restart",
"content":"because I said so"
}
}
}
With all of the above in mind, you could then do the following in JS:
push.onReceivedMessage = function(payload) {
var obj = JSON.parse(payload);
switch (obj.aps.data.type) {
case 'message':
// do something with messages
break;
case 'restart':
// do something else
break;
}
};