我正在 Xamarin Android 中开发一个应用程序,对于我正在使用 FCM 预发布包的通知:https://www.nuget.org/packages/Xamarin.Firebase.Messaging/
现在,如果我清理应用程序数据,则一切正常,OnTokenRefresh
事件被触发并生成一个新 token - 当我在此 token 上发送新通知时,通知将由OnMessageReceived()
中的设备 -
问题是,当我更改代码并再次运行应用程序时,如果我使用旧 token ,则会在发送通知时收到 NotRegistered
错误,但如果我去清理应用程序数据,然后触发 OnTokenRefresh(),生成新 token - 新 token 有效。
这里有类似的问题,但这是 GCM(我正在使用 FCM):
Google cloud message 'Not Registered' failure and unsubscribe best practices?
https://stackoverflow.com/a/36856867/1910735
https://forums.xamarin.com/discussion/65205/google-cloud-messaging-issues#latest
我的FCMInstanceIdService
[Service, IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class FCMInstanceIdService : FirebaseInstanceIdService
{
private string Tag = "FCMInstanceIdService";
public override void OnTokenRefresh()
{
var fcmDeviceId = FirebaseInstanceId.Instance.Token;
if (Settings.DeviceId != fcmDeviceId)
{
var oldDeviceId = Settings.DeviceId;
Settings.DeviceId = fcmDeviceId;
//TODO: update token on DB - Currently OnTokenRefresh is only called when: 1. App data is cleaned, 2. The app is re-installed
//_usersProvider.UpdateUserDeviceId(oldDeviceId, fcmDeviceId);
}
base.OnTokenRefresh();
}
}
我的消息接收服务:
[Service, IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class FCMListenerService : FirebaseMessagingService
{
private string Tag = "FCM_Listener_Service";
public override void OnMessageReceived(RemoteMessage message)
{
base.OnMessageReceived(message);
var notification = message.GetNotification();
var data = message.Data;
var title = notification.Title;
var body = notification.Body;
SendNotification(title, body);
}
private void SendNotification(string title, string body)
{
//TODO: Display notification to user
}
}
list :
<application android:label="TBApp" android:theme="@style/TBAppTheme">
<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>
如何在 Debug模式下强制刷新 FCM token ,这样我就不必在每次运行应用程序时删除应用程序数据?
最佳答案
由于此问题仅在调试应用程序时从 Visual Studio 运行应用程序时发生(不在部署到 PlayStore 的版本中),因此我暂时解决该问题的方法是创建以下服务:
[Service]
public class FCMRegistrationService : IntentService
{
private const string Tag = "FCMRegistrationService";
static object locker = new object();
protected override void OnHandleIntent(Intent intent)
{
try
{
lock (locker)
{
var instanceId = FirebaseInstanceId.Instance;
var token = instanceId.Token;
if (string.IsNullOrEmpty(token))
return;
#if DEBUG
instanceId.DeleteToken(token, "");
instanceId.DeleteInstanceId();
#endif
}
}
catch (Exception e)
{
Log.Debug(Tag, e.Message);
}
}
}
然后在我的启动事件(每当打开应用程序时加载的事件)执行以下操作:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
#if DEBUG
if (!IsMyServiceRunning("FCMRegistrationService"))
{
var intent = new Intent(this, typeof(FCMRegistrationService));
StartService(intent);
}
// For debug mode only - will accept the HTTPS certificate of Test/Dev server, as the HTTPS certificate is invalid /not trusted
ServicePointManager.ServerCertificateValidationCallback += (o, certificate, chain, errors) => true;
#endif
}
这将注销您现有的 FCMToken 并刷新 token ,因此将调用 OnTokenRefresh
方法,然后您必须编写一些逻辑来更新服务器上的 FCMToken。
[Service, IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class FCMInstanceIdService : FirebaseInstanceIdService
{
// private string LogTag = "FCMInstanceIdService";
public override void OnTokenRefresh()
{
var fcmDeviceId = FirebaseInstanceId.Instance.Token;
// Settings (is Shared Preferences) - I save the FCMToken Id in shared preferences
// if FCMTokenId is not the same as old Token then update on the server
if (Settings.FcmTokenId != fcmDeviceId)
{
var oldFcmId = Settings.FcmTokenId;
var validationContainer = new ValidationContainer();
// HERE UPDATE THE TOKEN ON THE SERVER
TBApp.Current._usersProvider.UpdateFcmTokenOnServer(oldFcmId, fcmDeviceId, validationContainer);
Settings.FcmTokenId = fcmDeviceId;
}
base.OnTokenRefresh();
}
}
关于c# - FCM - 重新调试应用程序后发送消息时出现 Android Xamarin NotRegistered 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40670587/