我正在使用https://github.com/zo0r/react-native-push-notification
来获取推送通知,当应用程序是Active
或在Background
我收到通知时,onNotification正如预期的那样工作,但是当我杀死应用程序时我只收到通知并且onNotification未被触发可以任何人请帮助我搜索一个解决方案,但没有任何工作,我正在增加Android徽章计数当onNotification被解雇是否有另一种方法来增加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服务将重新启动并收到消息。
您可以做的是安排apriori通知。例如,WhatsApp经常发布通知,例如:“当您强行关闭应用程序以提醒用户重新打开应用程序时,您可能会收到新通知”。
参考文献:
[1] https://stackoverflow.com/a/37845174/3607191
[2] https://stackoverflow.com/a/47857531/3607191
您可以在JS端调用无头功能来更新批次计数,以下是您必须执行的操作。在这里,您要编写一些Java代码。
FirebaseMessagingService
的服务。HeadlessJsTaskService
来调用另一个服务来调用无头JS函数。参考[3]的RNPushNotificationActionService.java
文件。onMessageReceived
函数中的[1]中所述,调用您在步骤2中编写的本机服务来调用无头JS函数。要知道怎么做,请参考[3]的RNPushNotificationActionHandlerReceiver.java
文件。