[英]OnTokenRefresh never called in Xamarin Android
I'm trying to implement remote notifications in my Xamarin Android project. 我正在尝试在我的Xamarin Android项目中实现远程通知。 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. 我按照各种指南中的逐步指示进行操作,但是OnTokenRefresh
方法从未被调用过,即使是第一次,所以看起来我的应用程序没有收到任何类型的令牌,甚至没有进行任何类型的firebase注册。
google-services.json
我已将Firebase添加到我的Android应用程序并下载了google-services.json
google-services.json
in my Xamarin Android Project and set the Build Action to GoogleServicesJson 我在我的Xamarin Android项目中包含了google-services.json
,并将Build Action设置为GoogleServicesJson I inserted this code in AndroidManifest.xml
我在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>
I created a class that extends FirebaseInstanceIdService
我创建了一个扩展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 } }
As I have already said OnTokenRefresh
is never called. 正如我已经说过OnTokenRefresh
从未被调用过。 What I can not understand is: who does the Firebase registration to receive a valid token? 我无法理解的是:Firebase注册谁接收有效令牌? Is there a missing directive? 有缺失的指令吗? Is there a missing method that makes this registration? 是否有丢失的方法进行此注册? Why OnTokenRefresh
is never called? 为什么从不调用OnTokenRefresh
?
You should start the service in your Activity like this : 您应该在Activity中启动服务,如下所示:
var intent = new Intent(this, typeof(FirebaseRegistrationService)); StartService(intent);
Also clean the solution once and run the app again. 同时清理解决方案并再次运行应用程序。 There is a bug in firebase for xamarin. xamarin的firebase中有一个错误。
According to this document , (see section titled: Implement the Firebase Instance ID Service) OnTokenRefresh is only called under a few circumstances: 根据此文档 ,(请参阅标题为:实施Firebase实例ID服务的部分)仅在以下几种情况下调用OnTokenRefresh:
In order to trigger OnTokenRefresh, you should first uninstall the app from the device. 要触发OnTokenRefresh,您应该首先从设备中卸载应用程序。 Then when you reinstall the app and open it for the first time, OnTokenRefresh will be called. 然后,当您重新安装应用程序并首次打开它时,将调用OnTokenRefresh。
You can avoid cleaning the solution every time by disabling 您可以通过禁用来避免每次都清理解决方案
Preserve application data cache on device between deploys 在部署之间保留设备上的应用程序数据缓存
under 下
Tools -> Options -> Xamarin -> Android Settings in VS2017 Enterprise. 工具 - >选项 - > Xamarin - > VS2017企业版中的Android设置 。
This made OnTokenRefresh
to be called after every redeploy. 这使得每次重新部署后都会调用OnTokenRefresh
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.