React Native集成FCM API时iOS无法检测DeepLink问题求助
iOS端FCM推送通知无法获取Deep Link的解决方案
1. 修改FCM API请求体
iOS的APNs与Android FCM对通知的处理逻辑不同,仅在data字段中放置gcm.n.link无法被iOS系统识别为跳转链接,需要调整请求体结构:
{ "to": "token", "notification": { "body": "body", "title": "title", "subtitle": "subtitle", "click_action": "myApp://?screenName=TradeDetail" }, "data": { "screenName": "TradeDetail" }, "apns": { "payload": { "aps": { "content-available": 1, "mutable-content": 1 }, "custom_link": "myApp://?screenName=TradeDetail" } } }
click_action:指定iOS点击通知后触发的URL Schemeapns.payload:配置APNs专属参数,确保通知能携带自定义链接字段content-available: 1:启用静默通知能力,保证通知点击事件能被App捕获
2. iOS端配置调整
(1)配置自定义URL Scheme
打开ios/[你的App名称]/Info.plist,添加如下配置:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>myApp</string> </array> </dict> </array>
(2)修改AppDelegate处理通知点击
以Objective-C为例,在AppDelegate.m中添加通知点击的处理逻辑,将链接传递给React Native的Linking模块:
#import <React/RCTLinkingManager.h> // 处理通知点击事件 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { NSDictionary *userInfo = response.notification.request.content.userInfo; NSString *link = userInfo[@"custom_link"] ?: userInfo[@"gcm.n.link"]; if (link) { [RCTLinkingManager openURL:[NSURL URLWithString:link] options:@{} completion:nil]; } completionHandler(); } // 处理App冷启动时的通知链接 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 原有代码... NSDictionary *notification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (notification) { NSString *link = notification[@"custom_link"] ?: notification[@"gcm.n.link"]; if (link) { [RCTLinkingManager openURL:[NSURL URLWithString:link] options:@{} completion:nil]; } } return YES; }
(3)确保开启推送权限
在Xcode中,进入项目Signing & Capabilities,添加Push Notifications权限;同时在App启动时请求用户的通知权限。
3. 优化React Native端链接监听逻辑
Linking.getInitialURL()仅处理App冷启动的场景,需要补充运行时的链接监听:
import { useEffect } from 'react'; import { Linking } from 'react-native'; useEffect(() => { // 处理冷启动链接 Linking.getInitialURL().then(initialUrl => { if (initialUrl) handleDeepLink(initialUrl); }); // 处理App运行时的链接 const urlListener = ({ url }) => handleDeepLink(url); Linking.addEventListener('url', urlListener); return () => Linking.removeEventListener('url', urlListener); }, []); const handleDeepLink = (url) => { console.log('获取到Deep Link:', url); // 解析screenName并处理页面跳转 };
内容的提问来源于stack exchange,提问作者shubham
相关产品推荐
相关产品推荐

