我在Android中卸载和安装应用程序时未生成FCM令牌

FCM令牌没有生成,是第一次获得FCM令牌。从下一次FCM令牌不出现时。我搜索了很多链接,但实际上没有一个起作用。

以下是我的FCM课程

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        Log.v("onNewToken", "onNewToken : "+s);

    }


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.v("onMessageReceived", "onMessageReceived : "+remoteMessage.getData());

    }
}

以下是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="XXXXXX">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />



        <service android:name=".MyFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>


    </application>

</manifest>

MyActivity onCreate()

String notificationToken = FirebaseInstanceId.getInstance().getToken();
        Toast.makeText(MainActivity.this, "Token : "+notificationToken, Toast.LENGTH_LONG).show();

        FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
            @Override
            public void onComplete(@NonNull Task<InstanceIdResult> task) {

                if(task.isSuccessful()){
                    String token = task.getResult().getToken();
                    Log.v("## token","token : "+token);
                }else{
                    Log.v("## token","token : "+task.getException().getMessage());
                }
            }
        });

1.FCM令牌始终是空的,除了第一次,我已经卸载并安装了该应用程序,尽管FCM令牌是空的。

2.何时调用Firebase onMessageReceived方法?

请帮我 提前致谢。

评论