英文:
FCM OnTokenRefresh() method is not called
问题
我试图从FCM获取设备令牌。这段代码在演示项目中运行正常,但当我尝试在我的项目中使用它时,OnTokenRefresh方法未被调用。我已经完美地设置了所有东西,但不知道为什么方法没有被调用。
谢谢。
FirebaseInstanceIDService.cs
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
[System.Obsolete]
public class FirebaseInstanceIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
// [START refresh_token]
[System.Obsolete]
public override void OnTokenRefresh()
{
// 获取更新的InstanceID令牌。
var refreshedToken = FirebaseInstanceId.Instance.Token;
Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
System.Diagnostics.Debug.WriteLine($"######Token###### : {refreshedToken}");
Xamarin.Forms.Application.Current.Properties["Fcmtocken"] = FirebaseInstanceId.Instance.Token ?? "";
Xamarin.Forms.Application.Current.SavePropertiesAsync();
}
// [END refresh_token]
}
MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.Window.RequestFeature(WindowFeatures.ActionBar);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
NotificationCenter.CreateNotificationChannel(new NotificationChannelRequest());
Xamarin.FormsMaps.Init(this, savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
FirebaseApp.InitializeApp(this);
LoadApplication(new App());
NotificationCenter.NotifyNotificationTapped(Intent);
}
英文:
I'm trying to get a device token from FCM.
This code is working in the demo project, but when I try to use it in my project, the OnTokenRefresh method is not called. I did all setup perfectily but i don't know why method is not called.
Thank you.
FirebaseInstanceIDService.cs
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
[System.Obsolete]
public class FirebaseInstanceIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
// [START refresh_token]
[System.Obsolete]
public override void OnTokenRefresh()
{
// Get updated InstanceID token.
var refreshedToken = FirebaseInstanceId.Instance.Token;
Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
System.Diagnostics.Debug.WriteLine($"######Token###### : {refreshedToken}");
Xamarin.Forms.Application.Current.Properties["Fcmtocken"] = FirebaseInstanceId.Instance.Token ?? "";
Xamarin.Forms.Application.Current.SavePropertiesAsync();
}
// [END refresh_token]
}
MainActivity.cs
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.Window.RequestFeature(WindowFeatures.ActionBar);
CrossCurrentActivity.Current.Init(this, savedInstanceState);
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
NotificationCenter.CreateNotificationChannel(new NotificationChannelRequest());
Xamarin.FormsMaps.Init(this, savedInstanceState);
ZXing.Net.Mobile.Forms.Android.Platform.Init();
FirebaseApp.InitializeApp(this);
LoadApplication(new App());
NotificationCenter.NotifyNotificationTapped(Intent);
}
答案1
得分: 1
你正在做的那个已经过时。
对于启动场景,你应该实现一个 Android.Gms.Tasks.IOnSuccessListener
,然后在某个地方注册该监听器,就像这样:
FirebaseApp.InitializeApp(context);
FirebaseMessaging.Instance.GetToken().AddOnSuccessListener(<你的监听器实例>);
然后将 FirebaseInstanceIdService 更改为 FirebaseMessagingService:
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public sealed class MyMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
// 你收到了一条通知
}
public override void OnNewToken(string token)
{
// 你获得了一个新的令牌
}
}
英文:
What you are doing there is obsolete.
For startup scenarios you should implement a Android.Gms.Tasks.IOnSuccessListener
somewhere and register that listener like so:
<!-- language-all: csharp -->
FirebaseApp.InitializeApp(context);
FirebaseMessaging.Instance.GetToken().AddOnSuccessListener(<your listener instance here>);
Then change FirebaseInstanceIdService to FirebaseMessagingService instead:
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public sealed class MyMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
// you got a notification
}
public override void OnNewToken(string token)
{
// you got a new token
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论