[英]React-native firebase foreground notifications are not showing
我们遇到了前台通知问题。 我们实现了一切(我假设正确),但由于某种原因,我们没有收到前台通知。 当应用程序关闭或最小化时,我们能够在后台接收推送通知。 这是我们发送通知的代码:
async createNotificationListeners() {
const channel = new firebase.notifications.Android.Channel(
'channelId',
'Channel Name',
firebase.notifications.Android.Importance.Max
).setDescription('A natural description of the channel');
firebase.notifications().android.createChannel(channel);
// the listener returns a function you can use to unsubscribe
this.unsubscribeFromNotificationListener = firebase.notifications().onNotification((notification) => {
if (Platform.OS === 'android') {
const localNotification = new firebase.notifications.Notification({
sound: 'default',
show_in_foreground: true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.android.setChannelId('channelId') // e.g. the id you chose above
.android.setSmallIcon('ic_stat_notification') // create this icon in Android Studio
.android.setColor('#000000') // you can set a color here
.android.setPriority(firebase.notifications.Android.Priority.High);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
} else if (Platform.OS === 'ios') {
const localNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.ios.setBadge(notification.ios.badge);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
}
});
// This listener triggered when notification has been received in foreground
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.displayNotification(title, body);
});
// This listener triggered when app is in backgound and we click, tapped and opened notifiaction
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.displayNotification(title, body);
});
// This listener triggered when app is closed and we click,tapped and opened notification
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
this.displayNotification(title, body);
}
}
componentWillUnmount() {
// this is where you unsubscribe
this.unsubscribeFromNotificationListener();
}
displayNotification(title, body) {
// we display notification in alert box with title and body
Alert.alert(
title, body,
[
{ text: 'Ok', onPress: () => console.log('ok pressed') },
],
{ cancelable: false },
);
}
我们已经在谷歌上搜索并在发布之前尝试了许多不同的步骤,但不幸的是,我们还没有设法解决这个问题。 如果有人知道该怎么做,将不胜感激
根据React Native Firebase 通知,您必须按照这种方式在后台和前台获取通知。
您来自后端的通知格式应该是这样的
{
"to":"fpyskooyTr6Ga0OkKoVafK:APA91bHZzVr8OU4og2saA_lyn5Lw3agP6c5EYVLTtNaThjoj3VdHXjyKpuZ-B2jfmX8p9fe24Ig6oVEjuOuI7eTxhRUN78WrJX3-T5lckt6sTGqmXTMS5Ga_DM-Wy6D94rEF2HaRsaPM",
"collapse_key": "type_a",
"notification": {
"body": "Your Order has dsdsdsd accepted",
"title": "Order has been dsdsdsd"
},
"data": {
"target_screen": "order_detail",
"wasTapped": "true",
"text_message": "Your Order has been accepted",
"id": "238",
"title": "Order has been Accepted"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.