0

I am using cordova plugin add phonegap-plugin-push plugin for push notification In forground notification works fine.and i can handle event also. When my app is in background then i got notification as well but on click of push notification my event is not fire. I am using below code

$cordovaPushV5.initialize(options).then(function() {
                // start listening for new notifications
                $cordovaPushV5.onNotification();
                // start listening for errors
                $cordovaPushV5.onError();

                // register to get registrationId
                if (PNdeviceToken == null) //becuase registration will be done only the very first 
                {
                    $cordovaPushV5.register().then(function(registrationId) {
                        // save `registrationId` somewhere;
                        window.localStorage.setItem('PNdeviceToken', registrationId);
                        $rootScope.fcmToken = registrationId;
                        console.log(registrationId)
                        alert("first time registered id -- " + registrationId)
                    })

                } else {
                    $rootScope.fcmToken = PNdeviceToken;
                    alert("already saved registered id -- " + $rootScope.fcmToken)
                }

            });


$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) {
                console.log(event)
                console.log(data)
                })

When i tap on background push notiction then $cordovaPushV5:notificationReceived event not fire, How can I solve this problem?

How can i handle background push notification event?

user3855589
  • 1,113
  • 2
  • 15
  • 43

1 Answers1

0

I had the Same issue and got it resolved it after 2 days of research.

Handling the notification events is same whether the app is in foreground or background.

We have to set "content-available" : "1" in the data field while pushing notifications. Else it wont call notificationReceived event if app is in background.

Also note this is not possible as of now through Google Firebase Console.

We have to send our custom payload messages (data or notification or both) seperately using any one of the firebase servers.

Detailed info can be found on the plugin's GitHub Docs Page on background notifications. Quoting from there -

On Android if you want your on('notification') event handler* to be called when your app is in the background it is relatively simple.

First the JSON you send from GCM will need to include "content-available": "1". This will tell the push plugin to call your on('notification') event handler* no matter what other data is in the push notification.

*on('notification') event handler = $cordovaPushV5:notificationReceived event in your case.

See this answer for sending custom payload messages using PHP and NodeJS

pro_cheats
  • 1,534
  • 1
  • 15
  • 25