Xamarin Form:Android 使用 Firebase Cloud Messaging 未收到消息 [英] Xamarin Form: Android using Firebase Cloud Messaging not receiving message

查看:39
本文介绍了Xamarin Form:Android 使用 Firebase Cloud Messaging 未收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我什至通过 Azure 的通知中心使 Firebase 云消息通知正常工作.我无法收到消息的时间是我尝试重新运行应用程序时.

I have made the Firebase Cloud Message Notification working even work through the Notification Hub from Azure. The time I can't receive the message is when I try to re-run the application.

流程:1) 使用 Visual Studio IDE 全新安装应用程序2) 停止调试器
3)通过Visual Studio IDE再次调试并运行应用程序4) 通过 FCM Console 发送测试消息

Process: 1) Fresh install the application with Visual Studio IDE 2) Stop the debugger
3) Debug and run the application again through Visual Studio IDE 4) Send a test message through FCM Console

如果我不做3,即使应用程序在后台我仍然可以收到消息

If I am not doing 3, I still can receive the message even if the application is in background

   LoadApplication(new App());

            FirebaseOptions options = new FirebaseOptions.Builder()
            .SetApplicationId(GetString(Resource.String.fcmAppId))
            .SetApiKey(GetString(Resource.String.fcmApiKey))
            .SetGcmSenderId(GetString(Resource.String.fcmGCMSenderId))
            .Build();

            FirebaseApp.InitializeApp(Android.App.Application.Context, options); 

?

    public class MyFirebaseInstanceIdService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseInstanceIdService";

    public override void OnTokenRefresh()
    {
        var refreshedToken = FirebaseInstanceId.Instance.Token;
        Settings.NotificationToken = refreshedToken;
        Android.Util.Log.Debug(TAG, "Refreshed token: " + refreshedToken);
        SendRegistrationToServer(refreshedToken);
    }

    void SendRegistrationToServer(string token)
    {
    }
}

最初我认为它有不同的令牌但相同.

Initially I thought it has a different token but is the same.

来自 Azure 门户的消息:

Message from Azure Portal:

从令牌提供者处获取的令牌有误.

The token obtained from the token provider is wrong.

推荐答案

Android 使用 Firebase Cloud Messaging 未收到消息

Android using Firebase Cloud Messaging not receiving message

正如 GCM 文档所说:

GCM 连接服务器向客户端应用发布的 ID,允许它接收消息.

An ID issued by the GCM connection servers to the client app that allows it to receive messages.

因此,只有当令牌可用时,您的应用才能接收来自 GCM 的消息.

So only when the token is available, your app can receive message from GCM.

应用如何获得该令牌?

您可以看到文档:

在您的应用首次启动时,FCM SDK 会为客户端应用实例生成一个注册令牌.如果您想定位单个设备或创建设备组,则需要访问此令牌.

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token.

每当生成新令牌时 onTokenRefreshcallback 都会触发,因此在其上下文中调用 getToken 可确保您访问当前可用的注册令牌.确保您已将该服务添加到清单中,然后在 onTokenRefresh 的上下文中调用 getToken

The onTokenRefreshcallback fires whenever a new token is generated, so calling getToken in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then call getToken in the context of onTokenRefresh

每次在设备中重新运行应用程序时,令牌都会更改,但作为 document 说,OnTokenRefresh 仅在系统确定令牌需要刷新时调用,它是密钥轮换和处理实例 ID 更改所必需的:

Every time when you re-run the application in your device the token is changed, but as the document said, OnTokenRefresh is only called when the system determines that the tokens need to be refreshed, it is needed for key rotation and to handle Instance ID changes due to :

  • 安装或卸载应用时.
  • 当用户删除应用数据时.
  • 当应用清除实例 ID 时.
  • 当令牌的安全性受到威胁时.

您需要触发OnTokenRefresh方法.您应该先从设备上卸载应用程序,然后重新安装应用程序并打开它,OnTokenRefresh 将被触发并更新令牌,您的应用程序可以收到GCM 消息再次.

You need to trigger OnTokenRefresh method. You should first uninstall the app from the device, then reinstall the app and open it, the OnTokenRefresh will be triggered and the token will be updated and your app could receive GCM message again.

这篇关于Xamarin Form:Android 使用 Firebase Cloud Messaging 未收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆