生成的FCM令牌为空,可能与使用的依赖项有些冲突

时间:2016-12-23 12:14:42

标签: android firebase-cloud-messaging

最初,当我为Android应用程序创建项目时,FCM已成功集成,并且我能够接收FCM令牌。但是当我在项目中添加更多功能和依赖项时,我将FCM令牌视为null。 以下是我对使用的依赖关系的看法

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile('org.apache.httpcomponents:httpcore:4.4.1') {
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'

    }    
    compile('com.google.api-client:google-api-client-android:1.22.0')
    compile('com.google.apis:google-api-services-script:v1-rev6-1.22.0') {
        exclude group: 'org.apache.httpcomponents'
    }

    compile 'pub.devrel:easypermissions:0.1.5'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.github.markushi:circlebutton:1.1'
    compile 'com.github.dmytrodanylyk.android-process-button:library:1.0.4'
    compile 'cn.pedant.sweetalert:library:1.3'
    compile 'me.spark:submitbutton:1.0.1'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.firebase:firebase-messaging:9.8.0'
    compile 'com.google.android.gms:play-services-auth:9.8.0'
    compile 'com.google.android.gms:play-services-appindexing:9.8.0'
    testCompile 'junit:junit:4.12'
    compile files('libs/org.apache.http.legacy.jar')
}

这是我的MyFirebaseInstanceIDService

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {

        Log.d(TAG,"Entered MyFirebaseInstanceIDService");
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);
    }
    // [END refresh_token]

    private void sendRegistrationToServer(String token) {
        // TODO: Implement this method to send token to your app server.
    }
}

这就是我记录FCM令牌的方式:

 String refreshedToken = FirebaseInstanceId.getInstance().getToken();
            Log.d(TAG, "Refreshed token: " + refreshedToken);

我认为这与google-api-client与FCM的依赖性存在冲突。有人可以指出,到底出了什么问题?

2 个答案:

答案 0 :(得分:0)

  只有在生成新令牌时才会调用FirebaseInstanceIdService中的

onTokenRefresh。如果您的应用程序先前已安装并生成令牌,则不会调用onTokenRefresh。尝试卸载并重新安装应用程序以强制生成新令牌,这将导致调用onTokenRefresh。

希望它能解决你的问题。快乐的编码。 请参阅此链接以了解FCM的实施情况。FireBase Cloud Messaging

答案 1 :(得分:0)

在Manifest.xml中的应用程序代码中,刚刚删除了 工具:node =“replace” ,它开始工作。