【发布时间】:2023-03-12 01:45:01
【问题描述】:
我在应用程序的版本 1 中实现了 UrbanAirship。
现在我在应用程序的第 2 版中扩展了 FirebaseMessagingService。
我没有接到onNewToken() 的电话,无法将令牌发送到我的服务器。
我的样板代码看起来像
AndroidManifest.xml
<service
android:name=".services.fcm.PushMessageReceiver"
android:enabled="true"
android:exported="true"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
和Receiver
public class PushMessageReceiver extends FirebaseMessagingService { ...
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
...
}
@Override
public void onNewToken(String s) {
Log.i(Config.LOGTAG, "######**** new token for fcm called");
Context ctx =ApplicationCustom.getContext();
SharedPreferences preferences = ctx.getSharedPreferences(Config.SHARED_PREFERENCES, Context.MODE_PRIVATE);
preferences.edit().putString(Config.SHARED_PREFS_DEVICE_TOKEN, s).apply();
Intent intent = new Intent(this, XmppConnectionService.class);
intent.setAction(XmppConnectionService.ACTION_FCM_TOKEN_REFRESH);
intent.putExtra("token", s);
startService(intent);
pushToServer();
}
public static void getToken() {
Log.i(Config.LOGTAG, "######**** get token for fcm called");
try {
Log.i(Config.LOGTAG, "######**** delete token for fcm called");
FirebaseInstanceId.getInstance().deleteInstanceId();
FirebaseInstanceId.getInstance().getInstanceId();
} catch (IOException e) {
e.printStackTrace();
Log.w(Config.LOGTAG, "######**** delete InstanceId failed", e);
}
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task
-> {
if (!task.isSuccessful()) {
Log.w(Config.LOGTAG, "getInstanceId failed", task.getException());
return;
}
Log.i(Config.LOGTAG, "######**** getInstanceId successful");
// Get new Instance ID token
String token = task.getResult().getToken();
Context ctx = ApplicationCustom.getContext();
SharedPreferences preferences = ctx.getSharedPreferences(Config.SHARED_PREFERENCES, Context.MODE_PRIVATE);
preferences.edit().putString(Config.SHARED_PREFS_DEVICE_TOKEN, token).apply();
pushToServer();
});
}
public void pushToServer(){
// Logic to push token to a server reading from preferences
}
}
观察:
1) 永远不会为正在更新的应用调用 onNewToken。
2) 新安装获得令牌
3) 在我添加了对FirebaseInstanceId.getInstance().deleteInstanceId() 的调用之后
OnComplete 也不会被调用。
4) 在真实手机(不是模拟器)上调用getToken(senderId, "FCM") 总是会导致
java.io.IOException: TOO_MANY_REGISTRATIONS
at com.google.firebase.iid.zzr.zza(Unknown Source:66)
at com.google.firebase.iid.zzr.zza(Unknown Source:79)
at com.google.firebase.iid.zzu.then(Unknown Source:4)
at com.google.android.gms.tasks.zzd.run(Unknown Source:5)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
我该如何修复观察 1. 是不是因为令牌已经交付给 UrbanAirship,所以没有调用 onNewToken? 仅供参考 getToken 在服务 onCreate() 方法中调用。
implementation 'com.google.firebase:firebase-messaging:17.3.4'
【问题讨论】:
-
你能解释一下这个错误是什么吗?
-
FCN Token问题是bug
标签: android firebase-cloud-messaging