使用 "react-native-firebase v6" 将数据从通知传递到它打开的应用程序的最佳方法是什么?
What is the best approach to passing data from a notification to the application that it opens, using "react-native-firebase v6"?
我想实现在与 Firebase 发送的推送通知交互时打开特定屏幕。我的理解是,我需要使用 getInitialNotification()
函数,该函数在 react-native-firebase v5
及更低版本中可用,但在 react-native-firebase v6
中尚不可用,因为通知包尚未准备就绪。
除其他外,到目前为止,我已经尝试设置云消息包中可用的后台消息处理程序,但它似乎不适用于不是 data-only
消息的东西:
Firebase.messaging().setBackgroundMessageHandler(async (remoteMessage) => {
await storeJSONData('notification', JSON.stringify(remoteMessage));
});
我应该降级到 react-native-firebase v5
以便从 Notifications 包中使用它们的 getInitialNotification()
,还是我有其他更好的选择,比如甚至使用 android 本机代码?
我最终通过在 firebase 云消息传递之上使用 react-native-push-notification
包并从中实现 onNotification
函数解决了这个问题。
PushNotification.configure({
onNotification: function(notification)
{
// process the notification
console.log('NOTIFICATION:', notification);
//iOS only
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});
我想实现在与 Firebase 发送的推送通知交互时打开特定屏幕。我的理解是,我需要使用 getInitialNotification()
函数,该函数在 react-native-firebase v5
及更低版本中可用,但在 react-native-firebase v6
中尚不可用,因为通知包尚未准备就绪。
除其他外,到目前为止,我已经尝试设置云消息包中可用的后台消息处理程序,但它似乎不适用于不是 data-only
消息的东西:
Firebase.messaging().setBackgroundMessageHandler(async (remoteMessage) => {
await storeJSONData('notification', JSON.stringify(remoteMessage));
});
我应该降级到 react-native-firebase v5
以便从 Notifications 包中使用它们的 getInitialNotification()
,还是我有其他更好的选择,比如甚至使用 android 本机代码?
我最终通过在 firebase 云消息传递之上使用 react-native-push-notification
包并从中实现 onNotification
函数解决了这个问题。
PushNotification.configure({
onNotification: function(notification)
{
// process the notification
console.log('NOTIFICATION:', notification);
//iOS only
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});