适用于 Windows 的 Visual Studio。
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) =>
{
};
}
}
}