FCM OnTokenRefresh()方法未被调用。

huangapple go评论67阅读模式
英文:

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(&lt;your listener instance here&gt;);

Then change FirebaseInstanceIdService to FirebaseMessagingService instead:

[Service(Exported = false)]
[IntentFilter(new[] { &quot;com.google.firebase.MESSAGING_EVENT&quot; })]
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
    }
}

huangapple
  • 本文由 发表于 2023年4月4日 14:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定