Xamarin Android中从未调用过OnTokenRefresh [英] OnTokenRefresh never called in Xamarin Android

查看:37
本文介绍了Xamarin Android中从未调用过OnTokenRefresh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Xamarin Android项目中实现远程通知.我按照各个指南中的分步说明进行操作,但是甚至第一次都没有调用 OnTokenRefresh 方法,因此看来我的应用程序没有收到任何种类的令牌,甚至没有任何类型的Firebase注册.

I'm trying to implement remote notifications in my Xamarin Android project. I followed step-by-step directions found in various guides but the OnTokenRefresh method is never called, even the first time, so it looks like my app does not receive any kind of token or even does not make any type of firebase registration.

  1. 我已将Firebase添加到我的Android应用中,并下载了 google-services.json
  2. 我在Xamarin Android项目中包含了 google-services.json ,并将构建操作"设置为GoogleServicesJson
  3. 我将此代码插入了 AndroidManifest.xml

  1. I have added Firebase to my Android app and downloaded google-services.json
  2. I included google-services.json in my Xamarin Android Project and set the Build Action to GoogleServicesJson
  3. I inserted this code in AndroidManifest.xml

<receiver 
android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" 
android:exported="false" />
<receiver 
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" 
android:exported="true" 
android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="${applicationId}" />
    </intent-filter>
</receiver>

  • 我创建了一个扩展 FirebaseInstanceIdService

    [Service]
    [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
    class FirebaseRegistrationService : FirebaseInstanceIdService
    {
    const string TAG = "FirebaseRegistrationService";
    
        public override void OnTokenRefresh()
        {
            var refreshedToken = FirebaseInstanceId.Instance.Token;
            System.Diagnostics.Debug.WriteLine(TAG, "Refreshed token: " + refreshedToken);
    
            MainActivity.CurrentActivity.RunOnUiThread(() => RegisterDeviceOnServer(refreshedToken));
        }
    
        public void RegisterDeviceOnServer(string refreshedToken)
        {
            // Custom implementation
        }
    }
    

  • 正如我已经说过的,从未调用过 OnTokenRefresh .我不明白的是:Firebase的注册人员是谁才能收到有效令牌?是否缺少指令?是否有缺少的方法可以进行此注册?为什么从未调用 OnTokenRefresh ?

    As I have already said OnTokenRefresh is never called. What I can not understand is: who does the Firebase registration to receive a valid token? Is there a missing directive? Is there a missing method that makes this registration? Why OnTokenRefresh is never called?

    推荐答案

    您应该像这样在活动"中启动服务:

    You should start the service in your Activity like this :

    var intent = new Intent(this,typeof(FirebaseRegistrationService));StartService(intent);

    也请清洁溶液一次,然后再次运行该应用程序.Fireabase中的xamarin有一个错误.

    Also clean the solution once and run the app again. There is a bug in firebase for xamarin.

    这篇关于Xamarin Android中从未调用过OnTokenRefresh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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