React Native推送通知深度链接:APP关闭时跳转失效求助
问题:APP完全退出后台后,点击推送通知无法跳转指定页面,仅打开首页
我需要实现点击推送通知跳转至指定页面的功能,目前的情况是:
- APP处于前台时:RN Firebase/messaging 和 OneSignal 均能正常跳转
- APP处于后台时:仅 OneSignal 能正常跳转
- APP完全退出后台后:点击通知只能打开APP首页,无法跳转到指定页面
1. index.js 代码
import {AppRegistry, Linking} from 'react-native'; import App from './App'; import {name as appName} from './app.json'; import messaging from '@react-native-firebase/messaging'; import notifee, {EventType} from '@notifee/react-native'; import {OneSignal} from 'react-native-onesignal'; const createNotificationChannels = async () => { await notifee.createChannel({ id: 'important', name: 'Important Notifications', importance: 4, }); await notifee.createChannel({ id: 'general', name: 'General Notifications', importance: 3, }); }; createNotificationChannels(); messaging().setBackgroundMessageHandler(async remoteMessage => { notifee.displayNotification({ title: remoteMessage.notification.title, body: remoteMessage.notification.body, android: { smallIcon: 'noti', color: '#134B7F', channelId: remoteMessage.data.channel, pressAction: {id: 'default'}, actions: [{title: 'TersanERP'}], }, data: remoteMessage.data, }); }); notifee.onBackgroundEvent(({type, detail}) => { switch (type) { case EventType.ACTION_PRESS: case EventType.PRESS: if (detail.notification.data.Linking) { Linking.openURL(detail.notification.data.Linking); } break; } }); messaging().onMessage(async remoteMessage => { notifee.displayNotification({ title: remoteMessage.notification.title, body: remoteMessage.notification.body, android: { smallIcon: 'noti', color: '#134B7F', channelId: remoteMessage.data.channel, }, data: remoteMessage.data, }); notifee.onForegroundEvent(({type, detail}) => { switch (type) { case EventType.ACTION_PRESS: case EventType.PRESS: if (detail.notification.data.Linking) { Linking.openURL(detail.notification.data.Linking); } break; } }); }); OneSignal.initialize('abcdef123465789-abcd-abcd-a1234-abcdef12345678'); OneSignal.Notifications.addEventListener('click', event => { Linking.openURL(event.notification.additionalData?.Linking); }); AppRegistry.registerComponent(appName, () => App);
2. React Navigation 深度链接配置
const linking = { prefixes: ['appurl://'], config: { screens: { HomeStackNavigator: { path: 'home', initialRouteName: 'Home', screens: { Home: '', UserEmployeeStack: { path: 'user-employee', screens: { Personel: 'personel', Details: 'details', VacationList: 'vacation-list', VacationFilter: 'vacation-filter', VacationAdd: 'vacation-add', VacationDetails: 'vacation-details', AvansList: 'avans-list', AvansDetails: 'avans-details', AvansFilter: 'avans-filter', AvansAdd: 'avans-add', }, }, ProjectStackNavigator: 'project', ManagerEmployeeStack: { path: 'manager-employee', screens: { Admin: 'admin', PersonelList: 'personel-list', VacationList: { path: 'vacation-list', parse: { show: show => show, }, }, VacationDetails: 'vacation-details', Details: 'details', VacationAdd: 'vacation-add', AdvanceAdd: 'advance-add', AdvanceList: 'advance-list', AdvanceDetails: 'advance-details', OvertimeScreen: 'overtime', }, }, WarehouseStack: 'warehouse', }, }, Profil: 'profile', Mesaj: 'message', }, }, };
3. RN Firebase 服务端(Node.js)代码
const admin = require("firebase-admin"); const serviceAccount = require("./appcred-abcd12-firebase-adminsdk-abcd124-abcd123.json"); admin.initializeApp({ credential: admin.credential.cert(serviceAccount), }); let usernames = ["USERNAME"]; usernames.forEach((username) => { admin .firestore() .collection("Tokens") .doc(username) .get() .then((doc) => { if (doc.exists) { let deviceregistrationToken = doc.data().token; const message = { token: deviceregistrationToken, data: { channel: "important", Linking: "appurl://home/manager-employee/vacation-list?show=waiting", }, notification: { body: "This is a notification message!", title: "Bildirim", }, android: { notification: { channelId: "important", color: "#134B7F", }, }, }; admin .messaging() .send(message) .then((response) => { console.log("Successfully sent message:", username); }) .catch((error) => { console.log("Error sending message:", error); }); } else { console.log("No such document!"); } }) .catch((error) => { console.error("Error retrieving user data: ", error); }); });
4. 环境信息
- React Native 版本:0.74.2
- 所有依赖包均为最新稳定版
- Android 项目配置:
buildToolsVersion = "34.0.0" minSdkVersion = 23 compileSdkVersion = 34 targetSdkVersion = 34 ndkVersion = "26.1.10909125" kotlinVersion = "1.9.22"
OneSignal 的配置逻辑和 RN Firebase/messaging 一致,两种方案都试过,但结果相同,肯定是遗漏了某些配置,寻求技术解决方法。
内容的提问来源于stack exchange,提问作者Mo3tasm
相关产品推荐
相关产品推荐

