FCM 令牌未在某些 android 设备中生成

FCM Token is not generating in some android devices

我在 GCM 中遇到问题,所以我迁移到 FCM,但我仍然遇到与令牌相关的问题。

我启动了更新后的应用程序并检查了有 300 个新用户, 其中 200 个正在获得令牌,而 100 个没有获得任何令牌,这是一个非常关键的问题。

有空令牌的解决方案吗?

您在每次全新安装时从 MyInstanceIDListenerService class onTokenRefresh() 方法获取 FCM 令牌。

或者你也可以通过这行代码获取FCM代币:

FirebaseInstanceId.getInstance().getToken();
     class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

            private static final String TAG = "MyFirebaseIIDService";

            @Override
            public void onTokenRefresh() {

                //Getting registration token
                String refreshedToken = FirebaseInstanceId.getInstance().getToken();

                //Displaying token in logcat
                Log.e(TAG, "Refreshed token: " + refreshedToken);

            }

            private void sendRegistrationToServer(String token) {
                //You can implement this method to store the token on your server
                //Not required for current project
            }
        }

     **Sometimes tokens are not generated then write this code**

public class ContCreateTokenService extends Service {
            @Nullable
            @Override
            public IBinder onBind(Intent intent) {
                return null;
            }

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                Intent serviceIntent = new Intent(this, MyFirebaseMessagingService.class);
                startService(serviceIntent);
                return START_REDELIVER_INTENT;
            }
        }

public class FetchNewRefreshToken extends IntentService {
    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     *
     * @param name Used to name the worker thread, important only for debugging.
     */

    public static final String TAG = FetchNewRefreshToken.class.getSimpleName();


    public FetchNewRefreshToken() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        try {

            // Resets Instance ID and revokes all tokens.
            FirebaseInstanceId.getInstance().deleteInstanceId();


            // Now manually call onTokenRefresh()
            Log.e(TAG, "Getting new token");
            FirebaseInstanceId.getInstance().getToken();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

同样添加到Manifest文件中

 <service android:name=".ContCreateTokenService" />
 <service android:name=".FetchNewRefreshToken " />

splash Activity 中:-

 startService(new Intent(this, FetchNewRefreshToken.class));
 startService(new Intent(this, ContCreateTokenService.class));