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

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

问题描述

我使Firebase Cloud Message Notification甚至可以通过Azure的Notification Hub正常工作.我无法收到消息的时间是当我尝试重新运行该应用程序时.

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控制台发送测试消息

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.

推荐答案

使用Firebase Cloud Messaging的Android未收到消息

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

每次在设备中重新运行该应用程序时,令牌都会更改,但是会作为

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时.
  • 令牌的安全性受到威胁时.
  • When the app is installed or uninstalled.
  • When the user deletes app data.
  • When the app erases the Instance ID.
  • When the security of the token has been compromised.

您需要触发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表单:使用Firebase Cloud Messaging的Android未收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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