onNotification() 在应用程序被杀死时不会触发



我正在使用https://github.com/zo0r/react-native-push-notification来获取推送通知,并且当应用程序Active或在我收到通知Background时,onNotification 正在按方面工作,但是当我杀死应用程序时,我只收到通知并且 onNotification 没有被触发,任何人都可以帮忙我搜索了一个解决方案,但没有任何效果, 触发 onNotification 时,我正在增加 Android 徽章计数,当应用程序被杀死时,有没有另一种方法可以增加 Android 徽章?

"react-native": "0.55.3",
"react-native-push-notification": "3.0.2"

我的代码

const options = {
// (optional) Called when Token is generated (iOS and Android)
onRegister: (token) => {
this._TOKEN = token.token;
this._addToPushChannels(channel);
},
onNotification: (notification) => {
console.log(notification);
if (notification['foreground']) {
}
//Notification from the background or when the app is killed
if (!notification['foreground']) {
if (Platform.OS !== 'ios' && Platform.Version < 26) {
this._messageCount++;
// console.log('this._messageCount ', this._messageCount);
let BadgeAndroid = require('react-native-android-badge');
BadgeAndroid.setBadge(this._messageCount);
}
}
// required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
notification.finish(PushNotificationIOS.FetchResult.NewData);
},
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: this._SENDER_ID,
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true,
};
PushNotification.configure(options);

我不是肯定的,但是当我记得正确杀死(别名强制关闭(时,应用程序会禁用Android(GCM(推送通知网关中的onNotification功能(与iOS/APNS不同(。这是一项Android明智的功能,并且是故意创建的,以便愿意强制关闭其应用程序的用户不会受到通知的"困扰"。Xtify 服务将重新启动,并在重新启动手机或重新打开应用程序时收到消息。

您可以做的是先验地安排通知。例如,WhatsApp经常在应用程序被强制关闭时发布诸如"您可能有新通知"之类的通知,以提醒用户重新打开应用程序。

引用:

  • GCM 推送通知在应用强制停止后工作?
  • https://github.com/firebase/quickstart-android/issues/378

[1] https://stackoverflow.com/a/37845174/3607191

[2] https://stackoverflow.com/a/47857531/3607191

[3] https://github.com/dluksza/react-native-push-notification/commit/5bb95881c27b0068249c0f68bbf65e54a565fa3d#diff-54ebb78785872f03fd8e71ed2dfed620R18

您可以在 JS 端调用无头函数来更新批处理计数,以下是您必须执行的操作。在这里,问题是你必须编写一些Java代码。

  1. 如 [2] 中所述,您必须编写一个扩展FirebaseMessagingService的服务。
  2. 通过扩展HeadlessJsTaskService来调用无头 JS 函数,再编写一个服务。请参考 [3]RNPushNotificationActionService.java文件。
  3. 现在,如 [1] 中所述onMessageReceived在服务的函数中调用您在步骤 2 中编写的本机服务来调用无头 JS 函数。要了解如何操作,请参阅 [3] 的文件RNPushNotificationActionHandlerReceiver.java

最新更新