无法接收来自Firebase的通知。未注册的注册令牌
我已经在Xamarin.Android应用程序中安装了Firebase,遵循以下指南: 一开始它似乎很有效,接收通知没有问题。然后我尝试更新代码并重新部署到设备,但之后什么也没发生。因为我无法从Firebase控制台接收任何通知 我试过了无法接收来自Firebase的通知。未注册的注册令牌,firebase,xamarin,xamarin.android,firebase-cloud-messaging,Firebase,Xamarin,Xamarin.android,Firebase Cloud Messaging,我已经在Xamarin.Android应用程序中安装了Firebase,遵循以下指南: 一开始它似乎很有效,接收通知没有问题。然后我尝试更新代码并重新部署到设备,但之后什么也没发生。因为我无法从Firebase控制台接收任何通知 我试过了 卸载应用程序并重新部署 从Firebase控制台下载新的google-service.json 删除并设置新的Firebase项目,并为此更新google-service.json 当应用程序是新部署的,并且我尝试发送通知时,什么也没有发生。更新和重新部
- 卸载应用程序并重新部署
- 从Firebase控制台下载新的google-service.json
- 删除并设置新的Firebase项目,并为此更新google-service.json
[Activity(Label = "<<App Label>>", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private TextView msgText;
private const string Tag = "MainActivity";
private FirebaseAnalytics _analytics;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_analytics = FirebaseAnalytics.GetInstance(this);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
msgText = FindViewById<TextView>(Resource.Id.msgText);
if (Intent.Extras != null)
{
foreach (var key in Intent.Extras.KeySet())
{
var value = Intent.Extras.GetString(key);
Log.Debug(Tag, "Key: {0} Value: {1}", key, value);
}
}
IsPlayServicesAvailable();
var logTokenButton = FindViewById<Button>(Resource.Id.logTokenButton);
logTokenButton.Click += delegate {
Log.Debug(Tag, "InstanceID token: " + FirebaseInstanceId.Instance.Token);
};
var subButton = FindViewById<Button>(Resource.Id.subscribeButton);
subButton.Click += SubButton_Click;
}
private void SubButton_Click(object sender, System.EventArgs e)
{
FirebaseMessaging.Instance.SubscribeToTopic("A");
Log.Debug(Tag, "Subscribed to A");
}
public bool IsPlayServicesAvailable()
{
int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.Success)
{
if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode))
msgText.Text = GoogleApiAvailability.Instance.GetErrorString(resultCode);
else
{
msgText.Text = "This device is not supported";
Finish();
}
return false;
}
else
{
msgText.Text = "Google Play Services is available.";
return true;
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="<<Package Name>>" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="16" />
<application android:label="<<App Label>>">
<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>
</application>
我碰到了同样的问题。再次尝试卸载应用程序,在获取令牌(旧令牌)之前,请调用刷新令牌以获取刷新令牌:
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}
void SendRegistrationToServer(string token)
{ // Your custom implementation depends on your server }
欲了解更多信息,请访问上述教程非常好,但遗漏了几点。我无法让它工作,直到我将我的应用程序的SHA1密钥添加到注册。您可以通过以下命令获得此信息:
"C:\Program Files\Java\jdk1.8.0_161\bin\keytool.exe" -list -v -keystore "%USERPROFILE%\AppData\Local\Xamarin\Mono for Android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
您可能需要稍微编辑该命令以匹配您的Java版本
第二件事是调试构建似乎存在一些问题。我从某处得到了这个方法(现在不知道在哪里)
在OnCreate之后从OnCreate调用它,如下所示:
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
ConfigureFireBase();
我正在Visual Studio for Mac(8.3.11)上使用Xamarin.Firebase.Messaging(71.1740.0)。在较旧(API 21)和较新(API 28)的物理android设备上进行测试 首先部署工作,两台设备上都有新令牌。对FCM的自定义REST API调用成功:
{"multicast_id":4160565375891761047,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1576702929647400%320d8b4c320d8b4c"}]}
更改代码,并在调试模式下重新部署到相同的设备上,效果良好。从Firebase控制台发送通知可以正常工作,在FCM中向同一项目应用程序中的两个设备发送通知
几个小时后,自定义REST API返回旧设备令牌(无代码更改):
再过几个小时,新设备令牌也会从自定义RESTAPI返回“NotRegistered”(无代码更改)。此外,Firebase控制台不再向设备发送通知
一旦发生这种情况,我获得自定义REST API和Firebase控制台以正确发送通知的唯一方法是使用Visual Studio从设备中手动删除应用程序:
Visual Studio Menu > Build > Remove from Device
再次部署将安装应用程序,就像它对设备是新的一样,并请求新的令牌
注意:已取消选中Visual Studio首选项中的“在应用程序部署之间保留数据/缓存”,没有重大更改。清除应用程序的数据/缓存也不起作用
使用了GitHub的最新Microsoft代码(2019年12月):Hi Martin。请发布相关代码片段。连同示例负载和完整的错误日志。重新安装应用程序时是否更新了令牌?@AL。我在原始帖子中添加了项目中的代码。@MikeMa每次重新安装应用程序时,我都会得到一个新令牌。您的清单缺少
Internet
权限。不管怎样,我已经面临同样的问题好几天了。我觉得软件包一定有问题,最近更新了一些错误。
{"multicast_id":4160565375891761047,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1576702929647400%320d8b4c320d8b4c"}]}
{"multicast_id":565695677811030352,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
Visual Studio Menu > Build > Remove from Device