我正在使用 firebase 向我的 React Native 应用程序发送推送通知。
我能够向我的设备发送通知。
我正在使用 https://fcm.googleapis.com/fcm/send 发送通知。下面是我发送到 api 的正文。
{
"to": "token",
"notification": {
"body": "body",
"title": "title",
"subtitle": "subtitle"
},
"data": {
"gcm.n.link": "myApp://?screenName=TradeDetail"
}
}
然后我使用链接来获取网址:
Linking.getInitialURL()
.then((initialUrl) => {
console.log("initialUrl: ", initialUrl);
})
.catch((err) => {
console.log("Error getting initial URL:", err);
});
我正在获取初始网址并能够将用户导航到其各自的屏幕。
当应用程序已经打开时就会出现问题,在这种情况下,当我单击通知时,什么也不会发生。
当应用程序已打开并且用户点击它时,如何检测通知中的 url?
如果您使用 notifee,它会提供可以完成这项工作的前台服务。检查这个Notifee
useEffect(() => {
const notificationSub = notifee.onForegroundEvent(({ type, detail }) => {
switch (type) {
case EventType.DISMISSED:
console.log('User dismissed notification', detail.notification);
break;
case EventType.PRESS:
console.log('User pressed notification', detail.notification);
break;
}
});
return notificationSub;
}, [])