在Android上的React Native应用程序中,当我在调试模式下运行我的应用程序时,一切运行正常,前台和后台的两个推送通知均收到正常,没有任何错误。但是,当我生成签名的APK文件时,仅接收到背景推送,而没有接收到前景推送。
我检查了android调试(Logcat),可以看到Firebase的通知正常,并且不在前台显示。
这是我的app.js代码,此代码有效。
// Firebase Cloud Messages Start
async componentDidMount() {
const notificationOpen: NotificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
const action = notificationOpen.action;
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(
JSON.stringify(notification.data, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
})
);
}
const channel = new firebase.notifications.Android.Channel(
"test-channel",
"Test Channel",
firebase.notifications.Android.Importance.Max
).setDescription("My apps test channel");
// Create the channel
firebase.notifications().android.createChannel(channel);
this.notificationDisplayedListener = firebase
.notifications()
.onNotificationDisplayed((notification: Notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
}); this.notificationListener = firebase
.notifications()
.onNotification((notification: Notification) => {
// Process your notification as required
notification.android
.setChannelId("test-channel")
.android.setSmallIcon("ic_launcher");
firebase.notifications().displayNotification(notification);
});
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened((notificationOpen: NotificationOpen) => {
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification: Notification = notificationOpen.notification;
var seen = [];
alert(
JSON.stringify(notification.data, function(key, val) {
if (val != null && typeof val == "object") {
if (seen.indexOf(val) >= 0) {
return;
}
seen.push(val);
}
return val;
})
);
firebase
.notifications()
.removeDeliveredNotification(notification.notificationId);
});}
componentWillUnmount() {
this.notificationDisplayedListener();
this.notificationListener();
this.notificationOpenedListener();
}
// END
我也收到这两个错误:
Could not find class ‘android.app.NotificationChannelGroup’, referenced from method io.invertase.firebase.notifications.RNFirebaseNotificationManager.parseChannelGroupMap
Could not find class ‘android.app.NotificationChannel’, referenced from method io.invertase.firebase.notifications.RNFirebaseNotificationManager.parseChannelMap