I'm using phonegap plugin push notification, it's working fine, what i'm looking is to execute an alert as long as new notification is received. for example:

push.on('notification', function(data) {
    alert('receive done');
}) 

so when i get new notification the alert doesn't pop up until no matter how long i surf the app, it doesn't show up until i click on the notification then after that it show up. what i'm looking for is a way to make that alert to show up when ever a new notification is received without clicking on the notification.

1 Answers 11

This is a big issue with push notifications on cordova. To handle the push right when you receive it, the push must be a silent/data only push. But if you want to notify your user you must use a local notification plugin. Unfortunately depending on which two plugins you are using they may conflict. I have found that using cordova-plugin-fcm works well with cordova-plugin-local-notifications.

With these you can handle silent/data only notifications immediately when they hit your device. The catch with the push plugin is that it does not show a notification on its own (if it is a silent/data push). You must use a local notification plugin if you want to handle receiving a push immediately while showing a notification to the user.

You can also check out my forks of the plugins (I have went through these same issues): local notifications and fcm. The local notification fork gives added functionality to android, and the fcm plugin shows a push notification while also letting the application handle the push right when it gets it (no click needed) without the need for another notification plugin.

Either way you choose to go, you must format your push like so:

{ "to":"[token]", "data":{ [data here] }, "priority":"high"}

Make sure you do not have a "notification" entry in the json, or else you will have the behaviour you are currently seeing.

upvote
  flag
ok thanks, but i don't really know how to make the push notifications silent/data only, and how to use a local notification plugin to notify the user, FCMPlugin.onNotification(function(data){ what should i write here to notify the user that he received a notification using local notification plugin }) . i hope you understand what i mean, thanks a lot – ler
upvote
  flag
To make the push silent, you need to make sure there is no 'notification' attribute in the content of the push, and to set the priority as 'high' (my answer example is the format you should follow). Within the FCMPlugin.onNotifictation() method you should call the .schedule() function on the localnotification plugin you are using here's the doc for that function – David Briglio
upvote
  flag
i tried your forks of this plugin 'cordova-plugin-fcm ' but it's not working anymore , whenever i get new notification i get that notification but the app stops it gives me 'Unfortunately, myappname has stopped.', i have another questions i hope you can tell me if it possible or not , i did post that question in here My question – ler
upvote
  flag
it's not working on ios, when i send an silent/data push to my iPhone i don't get the notification at all, but when i add "notification" entry in the json i get the notification, on android i get the notification in both ways but on ios i don't get the notification if i remove the "notification" entry in the json. – ler
upvote
  flag
Is this using my fork? If it is not than silent push will not be displayed. – David Briglio
upvote
  flag
i did fix that problem . i'm facing an other problem [my current problem ](//allinonescript.com/questions/47578913/…) . thanks in advance – ler

Not the answer you're looking for? Browse other questions tagged or ask your own question.