我使用fcm-package(https://github.com/evollu/react-native-fcm)将Firebase-Cloud-Messages发送到android .
消息到达手机 .
如果应用程序位于后台,则通知将显示在状态栏中 . 如果我点击该消息,应用程序将在主屏幕上打开 - 在我的情况下是News_Screen .
BUT 在应用程序到达前台后,我找不到捕获通知数据的方法 .
FCM.on(FCMEvent.Notification, (notif) => {
没有得到任何事件 .
如果应用程序位于前台,并且我发送了通知,则 console.log
-output正常工作 .
如果应用程序从后台到前台,我是否会错过能够获取事件数据的特殊内容?
This is the structure of my app:
index.js ⇒ /app/index.js ⇒ (imports a StackNavigator)
StackNavigator的默认屏幕是“News_Screen” .
在新闻屏幕我有这个:
async componentDidMount() {
// START Firebase Cloud Messaging
try {
let result = await FCM.requestPermissions({
badge: false,
sound: true,
alert: true
});
} catch (e) {
console.error(e);
}
FCM.getFCMToken().then(token => {
this.setState({token: token || ""});
console.log("First the token: ", token)
// store fcm token in your server
});
if (Platform.OS === "ios") {
FCM.getAPNSToken().then(token => {
console.log("APNS TOKEN (getFCMToken)", token);
});
}
FCM.on(FCMEvent.Notification, (notif) => {
if(notif.opened_from_tray){
setTimeout(()=>{
console.log("notif.opened_from_tray", notif);
}, 1000)
}
// there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
console.log("Message erhalten", notif);
if (notif.screen !== undefined) {
console.log("Jo, der Screen is", notif.screen, this.props.navigation);
this.props.navigation.navigate('Menu_Screen');
}
});
FCM.on(FCMEvent.RefreshToken, (token) => {
console.log("Again the token:", token)
// fcm token may not be available on first load, catch it here
});
}
我推送通知的唯一console.log,应用程序从后台到前台是 First the token...
消息 .
如何捕获通知的数据 . (例如,如果通知具有额外的Object-Parmete: screen: 'Menu_Screen
,则在App到达前景后切换到此屏幕)
1 回答
把你的听众变成这样的东西..
如果应用程序是前台,则此行NotificationType.WillPresent将捕获通知数据 .