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



我正在使用科尔多瓦插件添加电话间隙插件推送插件以进行推送通知 在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)
})

当我点击后台推送通知时,$cordovaPushV 5:通知已接收事件未触发,我该如何解决这个问题?

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

我遇到了同样的问题,经过 2 天的研究后解决了它。

无论应用在前台还是后台,处理通知事件都是相同的

我们必须在推送通知时在数据字段中设置">内容可用":"1"。否则,如果应用程序在后台,它不会调用notificationReceived事件。

另请注意,目前无法通过Google Firebase Console执行此操作。

我们必须使用任何一个 Firebase 服务器单独发送自定义有效负载消息(数据和/或通知)。

详细信息可以在插件的后台通知 GitHub 文档页面上找到。 从那里引用 -

在 Android 上,如果您希望在应用在后台运行时调用on('notification') 事件处理程序*,则相对简单。

首先,从 GCM 发送的 JSON 需要包含"内容可用":"1"。这将告诉推送插件调用您的on('notification')事件处理程序*,无论推送通知中还有什么其他数据。

*on('notification')事件处理程序 =$cordovaPushV5:notificationReceived事件。

请参阅此答案,了解如何使用PHPNodeJS发送自定义有效负载消息

最新更新