Android FCM推送通知点击后无法启动应用问题求助
问题解决步骤
1. 修正FCM推送请求Payload
FCM Legacy API中,notification字段下的click_action需要指定Intent Action名称,而非直接填URL。正确做法是将深度链接放在data字段,click_action设为自定义Action:
// POST https://fcm.googleapis.com/fcm/send { "to": "<device-token>", "notification": { "title": "Test!", "body": "testing", "mutable_content": true, "sound": "Tri-tone", "click_action": "OPEN_DEEP_LINK" // 自定义Action名称 }, "data": { "deep_link": "https://example.com/1234" // 存放深度链接 } }
2. 调整AndroidManifest配置
在MainActivity的<activity>标签内,添加匹配OPEN_DEEP_LINK Action的<intent-filter>,确保点击通知时能唤起MainActivity:
<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode" android:launchMode="singleTask" android:windowSoftInputMode="adjustResize" android:screenOrientation="portrait" android:exported="true"> <!-- 原有intent-filter保留 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="https" android:host="example.com" /> </intent-filter> <!-- 添加以下intent-filter匹配自定义Action --> <intent-filter> <action android:name="OPEN_DEEP_LINK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
注意:你之前添加的
TESTINGActivity配置是错误的——android:name必须指向项目中实际存在的Activity类(比如.MainActivity),而非自定义字符串,所以该配置无效,可直接删除。
3. 在React Native中处理通知点击的深度链接
在应用启动时(包括从通知点击唤起),获取Intent中的data字段,通过Linking处理深度链接:
import { Linking, useEffect } from 'react-native'; import messaging from '@react-native-firebase/messaging'; // 处理应用启动时的初始链接与通知数据 const handleNotificationLink = async () => { // 处理常规初始深度链接 const initialLink = await Linking.getInitialURL(); if (initialLink) { // 替换为你的深度链接处理逻辑 console.log('Initial deep link:', initialLink); } // 处理FCM通知点击传递的data const initialNotification = await messaging().getInitialNotification(); if (initialNotification?.data?.deep_link) { Linking.openURL(initialNotification.data.deep_link); } }; useEffect(() => { handleNotificationLink(); // 监听应用从后台唤起的链接事件 const linkSubscription = Linking.addEventListener('url', (event) => { console.log('Deep link from background:', event.url); // 替换为你的深度链接处理逻辑 }); return () => linkSubscription.remove(); }, []);
关键原理说明
- FCM推送的
notification字段是系统级通知,点击时会根据click_action匹配对应的Intent Filter; data字段的内容会随Intent传递到目标Activity,React Native可以通过Firebase Messaging或Linking获取;- 保持
MainActivity的launchMode="singleTask",确保应用在后台时不会重复启动实例,而是唤起现有实例并传递Intent数据。
内容的提问来源于stack exchange,提问作者xaunlopez
相关产品推荐
相关产品推荐

