Unity中Android定时通知点击无法唤起应用问题求助
Unity Android 定时通知无法唤起应用的问题排查与修复
核心问题定位
你碰到的问题是定时通知点击后错误唤起默认UnityActivity而非指定的MessagingUnityPlayerActivity,只有应用曾在前台运行过,关闭后点击通知才会正常唤起目标Activity。这基本不是Firebase SDK本身的bug,而是通知的PendingIntent配置、AndroidManifest设置或者Unity插件冲突导致的。
具体修复方案
1. 修正AndroidManifest.xml的Activity启动配置
确保MessagingUnityPlayerActivity是唯一的启动入口,移除默认UnityActivity的启动过滤器:
<!-- 设置MessagingUnityPlayerActivity为启动Activity --> <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:label="@string/app_name" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> <!-- 禁用默认UnityActivity的启动能力 --> <activity android:name="com.unityplayer.UnityActivity" android:enabled="false" android:exported="false"> <!-- 移除原有的MAIN/LAUNCHER intent-filter --> </activity>
2. 显式指定通知的点击目标Activity
不管是通过Firebase控制台还是后端API发送定时通知,都要在消息payload里明确指定click_action为MessagingUnityPlayerActivity的全类名,同时在Manifest中给该Activity添加对应的过滤器:
- Manifest新增过滤器:
<activity android:name="com.google.firebase.MessagingUnityPlayerActivity"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> <!-- 匹配payload中的click_action --> <action android:name="com.google.firebase.MessagingUnityPlayerActivity" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
- 后端payload示例(JSON):
{ "notification": { "title": "定时提醒", "body": "点击打开应用" }, "android": { "click_action": "com.google.firebase.MessagingUnityPlayerActivity", "notification": { "channel_id": "YOUR_CHANNEL_ID" } }, "to": "DEVICE_REGISTRATION_TOKEN" }
3. 排查Mobile Notification插件的配置冲突
你之前尝试用Mobile Notification修改Activity无效,需要确认插件是否覆盖了通知的PendingIntent设置:
- 打开Mobile Notification的配置面板,将Default Launch Activity设置为
com.google.firebase.MessagingUnityPlayerActivity - 关闭插件的自动通知通道创建功能,手动在Unity代码中构建通知时,显式绑定目标Activity的PendingIntent:
using Firebase.Messaging; using Android.App; using Android.Content; using AndroidX.Core.App; public class NotificationHandler : FirebaseMessagingService { public override void OnMessageReceived(RemoteMessage message) { base.OnMessageReceived(message); // 构建跳转Intent Intent intent = new Intent(this, Java.Lang.Class.ForName("com.google.firebase.MessagingUnityPlayerActivity")); intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop); PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable); // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "YOUR_CHANNEL_ID") .SetContentTitle(message.Notification.Title) .SetContentText(message.Notification.Body) .SetSmallIcon(Resource.Drawable.ic_notification) .SetContentIntent(pendingIntent) .SetAutoCancel(true); NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService); notificationManager.Notify(0, builder.Build()); } }
4. 版本兼容验证
你当前使用的Unity 2020.3.41f1和Firebase FCM 10.0.1,虽然官方标注兼容,但可以尝试升级Firebase SDK到Unity 2020支持的最新稳定版(比如11.0.0+),同时确保Android Build Tools版本在30.0.0以上,SDK Platform使用Android 11(API 30)或更高版本。
内容的提问来源于stack exchange,提问作者Vikas Reddy
相关产品推荐
相关产品推荐

