Firebase实例ID返回null

时间:2016-10-12 06:46:42

标签: android firebase push-notification firebase-cloud-messaging

我正在根据我在我的项目中使用google-services.json的文档集成Firebase的推送通知,如下所示。

顶级gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

项目级别Gradle

compileSdkVersion 24
    buildToolsVersion "24.0.2"

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        testCompile 'junit:junit:4.12'
        compile project(':humanapi-sdk')
        compile files('libs/Android_iwown_Ble.jar')
        compile('com.crashlytics.sdk.android:crashlytics:2.5.2@aar') {
            transitive = true;
        }

        compile 'com.android.support:support-compat:24.2.0'
        compile 'net.danlew:android.joda:2.9.2'
        compile 'com.android.support:multidex:1.0.1'
        compile 'com.android.support:appcompat-v7:24.2.0'
        compile 'com.android.support:design:24.2.0'
        compile 'com.android.support:cardview-v7:24.2.0'
        compile 'com.android.support:recyclerview-v7:24.2.0'
        compile 'com.android.support:customtabs:24.2.0'

        compile 'com.google.firebase:firebase-messaging:9.6.1'


    }

    apply plugin: 'com.google.gms.google-services'

在我的活动代码中

String token = FirebaseInstanceId.getInstance().getToken();
        Log.i(TAG,"Firebase token: "+token);

返回的标记始终为空

2 个答案:

答案 0 :(得分:0)

它完全错误!!!! 您只能在FirebaseInstanceIdService中获取ID!

它会自动撤销,无法手动完成。

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.
 */
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);
}
}

不要忘记将服务添加到您的清单中。

答案 1 :(得分:-1)

从firebase设置指南设置所有内容效果很好,但有时它不起作用。令牌未生成。

“在通过旧的FCM实施和生成清单挖掘之后,我现在可以说清单合并就是问题。

解决此问题的方法是将这些类手动添加到应用程序清单中“

<activity
    android:name="com.google.android.gms.common.api.GoogleApiActivity"
    android:exported="false"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

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

<service
    android:name="com.google.firebase.messaging.FirebaseMessagingService"
    android:exported="true" >
    <intent-filter android:priority="-500" >
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

        <category android:name="com.fc.debug" />
    </intent-filter>
</receiver>


<receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
    android:exported="false" />
<!--

-->
<service
    android:name="com.google.firebase.iid.FirebaseInstanceIdService"
    android:exported="true" >
    <intent-filter android:priority="-500" >
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>

<provider
    android:name="com.google.firebase.provider.FirebaseInitProvider"
    android:authorities="com.fc.debug.firebaseinitprovider"
    android:exported="false"
    android:initOrder="100" />

<receiver
    android:name="com.google.android.gms.measurement.AppMeasurementReceiver"
    android:enabled="true" >
    <intent-filter>
        <action android:name="com.google.android.gms.measurement.UPLOAD" />
    </intent-filter>
</receiver>

<service
    android:name="com.google.android.gms.measurement.AppMeasurementService"
    android:enabled="true"
    android:exported="false" />

参考:https://stackoverflow.com/a/40009448/5740236