Rey*_*bha 5 javascript android-notifications firebase react-native react-native-firebase
我有一个带有 react-native-firebase 通知的应用程序,消息已成功接收,通知已成功创建并显示在 Android Nougat 或更旧版本、Android Oreo 或更高版本上我添加了 channelId 它在使用 react-native run 时运行良好- android,但是当使用 bundle (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,
});
});
}
Run Code Online (Sandbox Code Playgroud)
那么这是显示通知的代码
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++);
}
}
Run Code Online (Sandbox Code Playgroud)