我们在前台通知方面遇到问题。我们实现了所有内容(我假设正确),但是由于某种原因,我们没有收到前台通知。当应用程序关闭或最小化时,我们可以在后台接收推送通知。这是我们发送通知的代码:
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 },
);
}
我们已经在Google上进行了搜索,并在发布之前尝试了许多不同的步骤,但是很遗憾,我们未能解决此问题。如果有人知道该怎么做,将不胜感激!