在此先感谢您的帮助。
当我在通知栏中单击通知时,我注意到android应用处于后台或关闭状态。当它打开应用程序时,即使在通知栏中显示了正确的正文,通知中的正文也会变得不确定。有人知道解决方法吗?
下面是我用于整个通知过程的代码
async componentDidMount() {
this.checkPermission();
this.createNotificationListeners();
}
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
async getToken() {
let value;
let fcmToken = await AsyncStorage.getItem('fcmToken', value);
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log("token = " + fcmToken)
await AsyncStorage.setItem('fcmToken', fcmToken);
}
} else {
console.log("stored token = " + fcmToken)
}
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
this.getToken();
} catch (error) {
}
}
componentWillUnmount() {
// this.notificationListener();
// this.notificationOpenedListener();
}
async createNotificationListeners() {
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { body } = notification;
this.showAlert( body);
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
console.log("notificationOpenedListener opened");
const { body } = notificationOpen.notification;
console.log(notificationOpen.notification);
this.showAlert( body);
});
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { body } = notificationOpen.notification;
this.showAlert( body);
}
this.messageListener = firebase.messaging().onMessage((message) => {
this.showAlert(message);
});
}
showAlert(message) {
Alert.alert(
'Notice',
message,
[
{ text: 'OK'},
],
{ cancelable: false },
);
}