我使用fcm-package(https://github.com/evollu/react-native-fcm)将Firebase-Cloud-Messages发送到android。
消息到达电话。
如果应用程序处于后台,则通知将显示在状态栏中。如果我点击该消息,则该应用程序将在主屏幕上打开-在我的情况下为News_Screen。
但是在应用程序出现在前台之后,我没有找到一种方法来捕获通知数据。FCM.on(FCMEvent.Notification, (notif) => {
没有任何事件。
如果该应用程序位于前台,并且我发送了通知,则console.log
-output工作正常。
如果应用程序从后台到前台,我是否会错过一些特殊的功能来获取事件数据?
,这是我的应用程序的结构: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...
消息。如何捕获通知数据。 (例如,如果Notification具有其他Object-Parmete:
screen: 'Menu_Screen
,则在App进入前台后切换到此屏幕)
最佳答案
将您的听众更改为这样的内容。
FCM.on(FCMEvent.Notification, notif => {
console.log("Notification", notif);
if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
notif.finish(WillPresentNotificationResult.All)
return;
}
if(Platform.OS ==='ios'){
switch(notif._notificationType){
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData)
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All)
break;
}
}
this.showLocalNotification(notif)
});
showLocalNotification(notif) {
console.log('here local', notif)
FCM.presentLocalNotification({
title: notif.title,
body: notif.body,
priority: "high",
show_in_foreground: true,
local: true
});
}
如果应用程序是前台,则该行NotificationType.WillPresent将捕获通知数据。
关于android - React-Native-Firebase Cloud Messaging-如果从任务栏打开则没有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425347/