创建Firebase消息时添加深度链接



我正在使用firebaseMessage类为react原生应用程序创建推送通知。我希望通知将用户带到应用程序的特定屏幕。现在点击推送通知只会将用户带到他们重新停飞应用程序之前的最后一个屏幕。我正在我的iOS设备上测试这个。如何在消息中嵌入特定的深层链接?我会使用setApnsConfig(ApnsConfig apnsConfig)还是setFcmOptions(FcmOptions fcmOptions)

我会使用APNS配置,因为这是用于iOS应用程序的:

https://firebase.google.com/docs/reference/admin/java/reference/com/google/firebase/messaging/ApnsConfig.Builder

你可以用不同的方式来处理它,但你可以在标题字段中包括一个URL,并将其用于自定义深度链接逻辑,或者你可以在putCustomData(String key, Object value)中有自定义数据,然后让你的应用程序处理该信息,以深度链接到你的应用程序的正确部分。

您的应用程序将在application(_:didReceiveRemoteNotification:fetchCompletionHandler:)中处理此通知

https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application

我在react native中制作的应用程序中也集成了这一功能。看看下面的解决方案。

正如你在应用程序中看到的那样,我正在等待通知,我会检查它是否有可用的类型。我的通知总是路由到另一个页面。在我的情况下,它是相同的页面,但我在有效负载中设置了一个路由作为数据类型。

之后,您可以将该路由有效载荷与您的导航程序库(在本例中为react-navigation(一起使用,以导航到正确的屏幕。

您应该选择哪种触发器最适合您的ether onNotificationOpenedApp或getInitialNotification。

useEffect(() => {
// Assume a message-notification contains a "type" property in the data payload of the screen to open
messaging().onNotificationOpenedApp((remoteMessage) => {
console.log(
"Notification caused app to open from background state:",
remoteMessage
);
//navigation.navigate(remoteMessage.data.type);
});
// Check whether an initial notification is available
messaging()
.getInitialNotification()
.then((remoteMessage) => {
if (remoteMessage) {
console.log(
"Notification caused app to open from quit state:",
remoteMessage
);
const route = remoteMessage?.data?.route;
navigation.navigate(route, {
data: remoteMessage.data,
});
}
})
.catch((error) => console.log("Caught ", error));
}, []);

最新更新