我有一个带有react-native-firebase通知的应用,成功接收到该消息,并且该通知已成功创建并显示在Android Nougat或更旧的版本上,在Android Oreo或更高版本上,我添加了channelId,在使用react-时效果很好本机运行android,但是当使用捆绑软件(cd android && ./gradlew installRelease)时,我的通知不仅显示在Android Oreo或更高版本的前台,而且在后台运行。不知道问题出在消息侦听器上还是在创建通知时。这是我的代码...
获得许可后,我在下面调用函数,以在应用程序处于前台状态时收听消息
export const listening_message = () => {
return firebase.notifications().onNotification((notification) => {
display_notification({
notification_param: {
sound: 'default',
show_in_foreground: true,
},
title: notification.title,
subtitle: notification.subtitle,
body: notification.body,
data: notification.data,
});
});
}
然后这是显示通知的代码
async function display_notification(parameter) {
let realmUser = await Realm.open(RealmData.User);
if(realmUser.objects(RealmDataName.User).length > 0) {
let realmNotification = await Realm.open(RealmData.NotificationID);
if(realmNotification.objects(RealmDataName.NotificationID).length == 0) {
realmNotification.write(() => {
realmNotification.create(RealmDataName.NotificationID, {
id: 0,
})
});
}
if(realmNotification.objects(RealmDataName.NotificationID).length == 0) {
realmNotification.write(() => realmNotification.create(RealmDataName.NotificationID, {id: 0}));
}
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Channel Name',
firebase.notifications.Android.Importance.High
).setDescription('Description of the channel');
firebase.notifications().android.createChannel(channel);
let new_notification = new firebase.notifications.Notification(parameter.notification_param)
.setNotificationId(realmNotification.objects(RealmDataName.NotificationID)[0].id.toString())
.setTitle(parameter.title)
.setSubtitle(parameter.subtitle)
.setBody(parameter.body)
.setData({
id: realmNotification.objects(RealmDataName.NotificationID)[0].id.toString(),
...parameter.data,
});
if(Platform.OS == "android") {
new_notification.android.setVibrate(1000)
.android.setSmallIcon('ic_launcher')
.android.setColor('#000000');
if(Platform.Version <= 25) {
new_notification.android.setPriority(firebase.notifications.Android.Priority.High);
} else {
new_notification.android.setChannelId("channelId");
}
} else if(Platform.OS == "ios") {
new_notification.ios.setBadge(1);
}
firebase.notifications().displayNotification(new_notification);
realmNotification.write(() => realmNotification.objects(RealmDataName.NotificationID)[0].id++);
}
}