如果单击通知,我想打开特定页面。如何在React Native中做到这一点?
this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
// Process your notification as required
// ANDROID: Remote notifications do not contain the channel ID.
// You will have to specify this manually if you'd like to re-display the notification.
const { title, body } = notification; }
);
const { title, body } = notification;
});
答案 0 :(得分:3)
async setupNotification() {
firebase.notifications().getInitialNotification()
.then((notificationOpen) => {
if (notificationOpen) {
// App was opened by a notification
// Get the action triggered by the notification being opened
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification = notificationOpen.notification;
}
});
const channel = new firebase.notifications.Android.Channel(
'channelId',
'ChannelName',
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) => {
console.log('local notifciation ' + JSON.stringify(notification.data))
try {
if (Platform.OS === 'ios') {
const localNotification = new firebase.notifications.Notification()
.setNotificationId(notification.notificationId)
.setTitle(notification.title)
.setSound('default')
.setSubtitle(notification.subtitle)
.setBody(notification.body)
.setData(notification.data)
.ios.setBadge(notification.ios.badge);
firebase.notifications()
.displayNotification(localNotification)
.catch(err => console.error(err));
// alert('Local notification')
}
} catch (error) {
alert(error)
}
});
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
// alert('Remote notification from killed state to foreground state')
// App was opened by a notification
// Get the action triggered by the notification being opened from killed state to foreground
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification = notificationOpen.notification;
if (notification.data) { // && Platform.OS !== 'ios'
setTimeout(() => {
this.handleNavigation(notification.data);
}, 100);
}
firebase.notifications().removeAllDeliveredNotifications();
}
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
// Get the action triggered by the notification being opened from background state to foreground
// alert('Remote notification from background state to foreground state')
const action = notificationOpen.action;
// Get information about the notification that was opened
const notification = notificationOpen.notification;
if (notification.data) { // && Platform.OS !== 'ios'
setTimeout(() => {
this.handleNavigation(notification.data);
}
}, 100);
}
//Remove delivered notifications from notification tray
firebase.notifications().removeAllDeliveredNotifications();
});
}
=========================
Handle Navigation Method
=========================
handleNavigation(notif) {
let type = notif.type ? notif.type : ''
if (type === 'library') {
this.props.navigation.navigate('ScreenName')
}
}
==== Called Setup Notification Method ====
constructor() {
super();
this.state = {};
this.setupNotification()
}