如何禁用Firebase通知自动展示,通过Unity组件实现自定义通知推送
禁用Firebase通知自动展示并自定义通知实现方案
Firebase云消息(FCM)默认会在应用退到后台时自动展示通知弹窗,要完全接管通知展示逻辑,按以下步骤操作即可:
步骤1:关闭Firebase通知自动展示配置
Android端配置
在AndroidManifest.xml的application节点下添加以下元数据,禁用FCM默认通知服务与自动初始化:
<meta-data android:name="firebase_messaging_auto_init_enabled" android:value="false" /> <meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />
iOS端配置
在Info.plist中添加以下配置项,禁用FCM自动通知展示:
<key>FirebaseMessagingAutoInitEnabled</key> <false/>
步骤2:接管OnMessageReceived全局回调
所有收到的FCM通知都会触发该回调,可在回调内提取全量通知数据:
using Firebase.Messaging; using System.Collections.Generic; public class FCMCallbackHandler : MonoBehaviour { private void Awake() { // 显式关闭自动初始化 FirebaseMessaging.DefaultInstance.AutoInitEnabled = false; // 绑定消息接收回调 FirebaseMessaging.MessageReceived += OnMessageReceived; } private void OnMessageReceived(object sender, MessageReceivedEventArgs e) { // 提取通知基础字段 string title = e.Message.Notification?.Title ?? "通知标题"; string content = e.Message.Notification?.Body ?? "通知内容"; // 提取自定义业务参数 IDictionary<string, string> customParams = e.Message.Data; // 调用自定义通知展示逻辑,完全不使用Firebase自带的通知展示能力 ShowCustomNotification(title, content, customParams); } }
步骤3:使用Unity组件展示自定义通知
方案A:使用Unity官方Mobile Notification组件
先在Package Manager中安装Mobile Notifications包,再按以下逻辑调用:
using Unity.Notifications.Android; using Unity.Notifications.iOS; private void ShowCustomNotification(string title, string content, IDictionary<string, string> customParams) { #if UNITY_ANDROID // Android 8.0+需要先注册通知通道 var notifyChannel = new AndroidNotificationChannel() { Id = "custom_fcm_channel", Name = "自定义通知通道", Importance = Importance.High, Description = "FCM自定义通知专属通道" }; AndroidNotificationCenter.RegisterNotificationChannel(notifyChannel); // 构造通知参数 var androidNotify = new AndroidNotification() { Title = title, Text = content, FireTime = System.DateTime.Now, SmallIcon = "notify_small_icon", // 替换为项目内图标名称 LargeIcon = "notify_large_icon" }; // 存入自定义参数,通知点击时可提取使用 foreach (var kv in customParams) { androidNotify.IntentData += $"{kv.Key}={kv.Value}&"; } AndroidNotificationCenter.SendNotification(androidNotify, "custom_fcm_channel"); #endif #if UNITY_IOS var iosNotify = new iOSNotification() { Title = title, Body = content, Identifier = System.Guid.NewGuid().ToString(), ShowInForeground = true, ForegroundPresentationOption = PresentationOption.Alert | PresentationOption.Sound }; // 存入自定义参数 foreach (var kv in customParams) { iosNotify.UserInfo.Add(kv.Key, kv.Value); } iOSNotificationCenter.ScheduleNotification(iosNotify); #endif }
方案B:使用UTNotification组件
直接调用UTNotification暴露的API即可:
private void ShowCustomNotification(string title, string content, IDictionary<string, string> customParams) { UTNotification.Instance.SendNotification( title: title, content: content, delaySeconds: 0, extraData: customParams ); }
注意事项
- 建议服务端全量发送**数据类型(data message)**FCM通知,不要发送通知类型(notification message),避免部分系统场景下回调被拦截。
- 需在应用启动时提前向用户申请通知权限,否则自定义通知无法正常展示。
内容的提问来源于stack exchange,提问作者Joel Oliveira
相关产品推荐
相关产品推荐

