Xamarin Android端CrossFirebasePushNotification的OnNotificationOpened事件不触发
根据你提供的代码和场景,这个问题基本都是由配置缺失、方法重写错误、事件引用被回收这几个具体问题导致的,按以下步骤逐一排查修改即可解决:
1. 修正SplashActivity的方法重写逻辑
你目前写的OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)是Android 5.0(API 21)以上才会触发的重载,正常冷启动、应用后台点击通知启动的场景下,系统实际调用的是不带PersistableBundle参数的基础OnCreate(Bundle)重载。你当前的代码里这个基础方法根本没重写,自然不会执行FirebasePushNotificationManager.ProcessIntent,也就无法把通知打开的 intent 传递给插件触发事件。
正确写法:
- 重写全版本兼容的基础
OnCreate(Bundle)方法,把ProcessIntent调用放在这个方法里 - 保留你原来的带
PersistableBundle的重载即可,不需要重复写ProcessIntent逻辑,因为基础OnCreate会被优先调用 - 启动页跳转MainActivity的逻辑写在
ProcessIntent调用之后,避免打断处理流程
参考代码:
[Activity(Label = "你的应用名称", Theme = "@style/SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, NoHistory = true)] public class SplashActivity : AppCompatActivity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // 无论什么系统版本启动,都会走到这里 FirebasePushNotificationManager.ProcessIntent(this, Intent); // 原有启动跳转逻辑 StartActivity(new Intent(this, typeof(MainActivity))); Finish(); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); // 应用在后台时点击通知,会走到这里 FirebasePushNotificationManager.ProcessIntent(this, intent); } }
2. 检查SplashActivity的启动模式配置
你现在重写了OnNewIntent方法,但这个方法只有在Activity的启动模式设置为SingleTop或SingleTask时才会被系统调用。如果用默认的Standard启动模式,当应用在后台收到通知时,系统会新建一个SplashActivity实例,根本不会走已存在实例的OnNewIntent逻辑,导致ProcessIntent不执行。
按照上面代码里的特性标记,给SplashActivity加上LaunchMode = LaunchMode.SingleTop,同时加NoHistory = true避免启动页残留在返回栈里即可。
3. 修复事件订阅被垃圾回收的问题
CrossGeeks的推送插件内部对事件的订阅是使用弱引用实现的,如果你在MainApplication.OnCreate里直接用匿名lambda订阅事件,很容易在后续GC回收过程中把委托回收掉,导致事件无法触发。
正确的订阅方式是把事件处理逻辑写成类的成员方法,用强引用进行订阅:
public class MainApplication : Application { // 类级别方法作为事件处理器,不会被弱引用回收 private void OnTokenRefresh(object sender, CrossFirebasePushNotification.TokenEventArgs e) { // 原有逻辑 } private void OnNotificationReceived(object sender, CrossFirebasePushNotification.NotificationEventArgs e) { // 原有逻辑 } private void OnNotificationOpened(object sender, CrossFirebasePushNotification.NotificationResponseEventArgs e) { // 原有打开通知的逻辑 } public override void OnCreate() { base.OnCreate(); CrossCurrentActivity.Current.Init(this); // 用类方法订阅,替换原来的匿名lambda CrossFirebasePushNotification.Current.OnTokenRefresh += OnTokenRefresh; CrossFirebasePushNotification.Current.OnNotificationReceived += OnNotificationReceived; CrossFirebasePushNotification.Current.OnNotificationOpened += OnNotificationOpened; } }
4. 快速定位问题的调试技巧
按照修改完的逻辑,打断点逐行确认:
- 把应用从最近任务列表完全划掉(模拟冷启动场景),从Firebase控制台发测试推送,点击通知后,看SplashActivity里
ProcessIntent的断点能不能命中- 如果断点不命中:检查是不是有其他Activity被误标了
MainLauncher = true,抢了SplashActivity的启动入口 - 如果断点命中但
OnNotificationOpened断点不进:就是事件订阅被回收的问题,换成上面说的强引用方式即可
- 如果断点不命中:检查是不是有其他Activity被误标了
- 保持应用在前台/后台运行的状态,点击通知看能不能触发事件,如果前台能触发、冷启动不触发,就是
OnCreate重写的逻辑漏了
补充说明:如果你的推送只通过Firebase控制台发送notification类型的基础消息,要确保推送内容里没有自定义的跳转目标,插件默认会把通知的点击事件绑定到你的
MainLauncher标记的Activity上,自定义的跳转目标会导致插件无法接收到打开事件。
内容的提问来源于stack exchange,提问作者markpsmith

