android - FCM 代币发行

标签 android firebase-cloud-messaging

我在应用程序的版本 1 中实现了 UrbanAirship。 现在,我在应用的版本 2 中扩展了 FirebaseMessagingService。 我没有在 onNewToken() 中收到能够将 token 发送到我的服务器的调用。 我的样板代码看起来像

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>

接收器

        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) 新安装获得 token

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. 是否因为 token 已经交付给UrbanAirship,所以onNewToken 没有被调用? 仅供引用 getToken 在服务 onCreate() 方法中调用。

implementation 'com.google.firebase:firebase-messaging:17.3.4'

最佳答案

您可以通过以下方式获取 fcm token :-

FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (task.isSuccessful()) {
                            String firebaseToken = task.getResult().getToken();
                        } else {
                            getFirebaseToken();
                        }
                    }
                });

关于android - FCM 代币发行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235190/

相关文章:

android - 我是否获得了验证用户的 Android 应用内订阅的正确步骤?

Android SSL 主机名验证 : DefaultHostnameVerifier vs BrowserCompatHostnameVerifier

android - 为什么我的计时器 Observable 从未被调用?

Angular 无法注册 ServiceWorker : A bad HTTP response code (404) was received when fetching the script

android - 在 : 'error: cannot find symbol variable cordova' 中检索 cordova 上下文结果

java - Android Build Strings 让我抓狂

android - Lollipop 上未着色的小部件

使用 FCM 的 iOS 推送通知

android - 在 Firebase Cloud Messaging 中,消息 ID 是什么以及我们在哪里使用 addData()

javascript - 错误 : function crashed out of request scope Function invocation was interrupted. 使用大列表