我在我的react本机项目中使用了推送通知,从推送通知中我获得了ID,但是前两次显示“未定义”,所以我的另一个导航器调用和条件失败。
我为推送通知创建了新的stacknavigator,但是每次预订失败时都带有预订ID,
componentWillUnmount() {
this.createNotificationListeners();
}
这是我的推送通知功能:
async createNotificationListeners() {
const channel = new firebase.notifications.Android.Channel(
'appname',
'appname',
firebase.notifications.Android.Importance.Max
).setDescription('my beauty stylist');
firebase.notifications().android.createChannel(channel);
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body, id } = notification.data;
console.log("Notification Title: "+title);
console.log("Notification body: "+body);
console.log("Notification id: "+id);
this.setState({ notificationscreen: true })
this.bookingId = id;
const localNotification = new firebase.notifications.Notification({
sound: 'default',
show_in_foreground: true,
})
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setBody(notification.body)
.setData(notification.data)
.android.setAutoCancel(true)
.android.setChannelId('mybeautysquad') // e.g. the id you chose above
.android.setSmallIcon('ic_launcher') // 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));
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body, id } = notificationOpen.notification.data;
console.log("Pubg",notificationOpen.notification.data);
this.setState({ notificationscreen: true })
this.bookingId = id;
// this.showAlert(title, body, id);
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body, id } = notificationOpen.notification.data;
this.setState({ notificationscreen: true })
this.bookingId = id;
// this.showAlert(title, body, id);
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log(JSON.stringify(message));
});
}
这是我的渲染代码
render() {
console.log("app js "+this.bookingId);
(Output: app js undefined)
console.log("notificationscreen: "+notificationscreen);
(Output: app js undefined)
const { checkedSignIn, signedIn, notificationscreen } = this.state;
if (!checkedSignIn) {
return null;
}
if(notificationscreen && this.bookingId != "undefined"){
return <AppStackNavigatorNew screenProps={{ bookingId: this.bookingId }} />;
}else{
return <AppStackNavigator1 screenProps={{ bookingId: this.bookingId }} />;
}
}
它不会是未定义的,如果我每次都收到推送通知,它应该移至推送通知导航。