CrossFirebasePushNotification.Current.OnTokenRefresh启动时约1/3概率不触发求助
问题:CrossFirebasePushNotification.OnTokenRefresh启动时触发概率异常
使用Windows版Visual Studio开发Xamarin.Forms应用时,CrossFirebasePushNotification.Current.OnTokenRefresh事件在应用启动时约有1/3的概率不会触发,该问题在调试模式及设备离线运行场景下均存在。
相关代码如下:
App.xaml.cs
public partial class App : Application { public App() { InitializeComponent(); CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh; } private void Current_OnTokenRefresh(object source, FirebasePushNotificationTokenEventArgs e) { System.Diagnostics.Debug.WriteLine($"Token: {e.Token}"); } }
MainActivity.cs
namespace MyApp.Droid { [Activity(Label = "MyApp", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )] public partial class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { partial void OnPreCreate(Bundle savedInstanceState); protected override void OnCreate(Bundle savedInstanceState) { this.OnPreCreate(savedInstanceState); TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); FirebasePushNotificationManager.ProcessIntent(this, Intent); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); LoadApplication(new App()); this.OnPostCreate(savedInstanceState); } protected override void OnNewIntent(Intent intent) { base.OnNewIntent(intent); FirebasePushNotificationManager.ProcessIntent(this, intent); } } }
Android项目Application.cs
namespace MyApp.Droid { [Application] public class MainApplication : Application { public override void OnCreate() { base.OnCreate(); //Set the default notification channel for your app when running Android Oreo if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { //Change for your default notification channel id here FirebasePushNotificationManager.DefaultNotificationChannelId = "FirebasePushNotificationChannel"; //Change for your default notification channel name here FirebasePushNotificationManager.DefaultNotificationChannelName = "General"; FirebasePushNotificationManager.DefaultNotificationChannelImportance = NotificationImportance.High; } //If debug you should reset the token each time. #if DEBUG FirebasePushNotificationManager.Initialize(this, true); #else FirebasePushNotificationManager.Initialize(this, false); #endif //Handle notification when app is closed here CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) => { }; } } }
解决方案建议
- 调整事件绑定时机:将
OnTokenRefresh事件的绑定从App构造函数移至App的OnStart方法中,确保Firebase初始化完成后再绑定事件,避免因初始化未完成导致事件丢失:protected override void OnStart() { base.OnStart(); CrossFirebasePushNotification.Current.OnTokenRefresh += Current_OnTokenRefresh; // 主动触发Token获取 _ = CrossFirebasePushNotification.Current.GetTokenAsync(); } - 主动获取Token:在绑定事件后主动调用
GetTokenAsync()方法,即使OnTokenRefresh未自动触发,也能主动获取Token,部分情况下该方法会在Token存在时触发OnTokenRefresh事件。 - 检查初始化日志:在
MainApplication的OnCreate方法末尾、App的事件绑定后添加日志输出,确认Firebase初始化状态与事件绑定状态,便于定位问题:// MainApplication中Initialize后添加 System.Diagnostics.Debug.WriteLine("Firebase Push Notification初始化完成"); // App的OnStart中绑定事件后添加 System.Diagnostics.Debug.WriteLine("OnTokenRefresh事件已绑定"); - 处理Token缓存逻辑:Release模式下
Initialize的第二个参数为false(不重置Token),若本地已有缓存Token,OnTokenRefresh不会触发。可在启动时主动检查Token是否为空,为空时再调用GetTokenAsync():var existingToken = CrossFirebasePushNotification.Current.Token; if(string.IsNullOrEmpty(existingToken)) { _ = CrossFirebasePushNotification.Current.GetTokenAsync(); } else { // 直接使用已有Token,或手动触发事件处理逻辑 Current_OnTokenRefresh(null, new FirebasePushNotificationTokenEventArgs(existingToken)); } - 确认权限配置:检查Android项目的
AndroidManifest.xml是否正确配置了Firebase所需权限,确保网络权限(INTERNET)及通知权限已添加,避免因权限问题导致Token获取失败。
内容的提问来源于stack exchange,提问作者Y U
相关产品推荐
相关产品推荐

