我正在 Xamarin Android 中开发一个应用程序,对于通知,我使用 FCM 预发布包:https://www.nuget.org/packages/Xamarin.Firebase.Messaging/
现在,如果我清理应用程序数据,一切都会正常工作,OnTokenRefresh
事件被触发并生成一个新令牌 - 当我在此令牌上发送新通知时,设备将发送和接收通知OnMessageReceived()
-
问题是当我更改代码并再次运行应用程序时,如果我使用旧令牌,我会得到NotRegistered
发送通知时出错,但如果我去清理应用程序数据,则会触发 OnTokenRefresh() 并生成新令牌 - 新令牌有效。
这里有类似的问题,但这是 GCM(我正在使用 FCM):
Google 云消息“未注册”失败和取消订阅最佳实践?
https://stackoverflow.com/a/36856867/1910735
https://forums.xamarin.com/discussion/65205/google-cloud-messaging-issues#latest
My 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
}
}
显现:
<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>
如何在调试模式下强制刷新 FCM 令牌,以便不必在每次运行应用程序时删除应用程序数据?