android - React-Native - Firebase 云消息传递 - 如果从托盘打开则没有事件
问题描述
我使用 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 将捕获通知数据。
推荐阅读
- kubernetes - 如何将 GKE 与在不同项目的 VM 上运行的外部数据库连接?
- jenkins - 在詹金斯中添加服务挂钩时出错。需要权限才能执行此操作“创建 StrongBox 抽屉”
- nginx - 如何根据 nginx 中的子域将流量路由到不同的端口?
- object - 需要传递哪个值来设置标准帐户对象的父子关系
- java - Spring WebClient 缓冲列表缺少最后一个信号?
- react-native - React Native Reanimated ScrollView 无限水平
- python - 无法在 Django 中自定义主键模式
- linker - 我们如何将 IAR Embedded Workbench 生成的输出与 4 字节(或任何其他所需值)对齐?
- ios - 为什么我不能将 GitHub 模块导入 Xcode?
- docker - iptables 如何在 docker 网络上运行