无法在后台推送通知中处理事件

时间:2017-06-15 09:12:23

标签: android cordova ionic-framework

我正在使用cordova插件添加phonegap-plugin-push插件进行推送通知 在forground通知工作正常。我也可以处理事件。 当我的应用程序在后台时,我也收到通知,但点击推送通知我的事件不是火。 我使用下面的代码

$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)
                })

当我点击后台推文时,然后$ cordovaPushV5:notificationReceived事件不会激活,我该如何解决这个问题呢?

如何处理后台推送通知事件?

1 个答案:

答案 0 :(得分: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