FirebaseInstanceIdService为gettoken返回nullFirebaseIn

2019-05-12 08:16发布

我试着去FCM通知添加到我的应用程序,但由于某种原因

FirebaseInstanceId.getInstance().getToken();

在返回null。

由于IM没有得到任何堆栈跟踪我最好的猜测是,FirebaseInstanceIdService不工作

  • 谷歌Play服务版本:9.6.1
  • 火力地堡的消息版本:9.6.1
  • 从FirebaseConsole加入JSON配置文件(试了单个文件与2个客户端在其内部,一个用于调试和一个用于发布版本)和2文件分别用于相应的构建

IW谷歌应用服务的插件在我的模块底部gradle这个脚本包含在项目的Google服务的Gradle根脚本

创建从officail文档2个服务:

public class MyFirebaseInsanceIDService  extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    PreferencesHelper.putSharedPreferencesString(Constants.User.PUSH_NOTIFICATIONS, refreshedToken);

    Log.e("TOKEN", "Token: " + FirebaseInstanceId.getInstance().getToken());
}

}

和清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.fc.test">

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

<application
    android:name="fctest"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="${appName}${appNameSuffix}"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:node="replace">

    <service
        android:name="com.fc.test.MyFirebaseInsanceIDService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
        </intent-filter>
    </service>

    <service
        android:name="com.fc.test.MyFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <activity
        android:name="com.fc.test.view.splash.Splash"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.CenterAnimation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</application>

根摇篮:

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        //noinspection GradleDynamicVersion
        classpath 'io.fabric.tools:gradle:1.+'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url "http://dl.bintray.com/drummer-aidan/maven" }
        maven { url "https://maven.fabric.io/public" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://jitpack.io" }
    }
}

ext {
    buildToolsVersion = '24.0.1'
    compileSdkVersion = 24
    minSdkVersion = 15
    targetSdkVersion = 24
    supportLibraryVersion = '24.2.1'
}

和模块的主要部分摇篮

    dependencies {
    final PLAY_SERVICES_VERSION = '9.6.1'
    final SUPPORT_LIBRARY_VERSION = '24.2.1'
    final RETROFIT_VERSION = '2.1.0'
    final DAGGER_VERSION = '2.5'
    final DEXMAKER_VERSION = '1.4'
    final HAMCREST_VERSION = '1.3'
    final ESPRESSO_VERSION = '2.2.1'
    final RUNNER_VERSION = '0.4'
    final BUTTERKNIFE_VERSION = '8.1.0'
    def daggerCompiler = "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
    def jUnit = "junit:junit:4.12"
    def mockito = "org.mockito:mockito-core:1.10.19"
    // App Dependencies
    compile "com.google.android.gms:play-services-gcm:$PLAY_SERVICES_VERSION"
    compile "com.google.firebase:firebase-messaging:$PLAY_SERVICES_VERSION"
    compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION"
    compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
    compile "com.android.support:cardview-v7:$SUPPORT_LIBRARY_VERSION"
    compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"
    compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
    compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
    compile "com.squareup.retrofit2:retrofit:$RETROFIT_VERSION"
    compile "com.squareup.retrofit2:converter-gson:$RETROFIT_VERSION"
    compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION"
    compile "com.jakewharton:butterknife:$BUTTERKNIFE_VERSION"
    compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
        transitive = true;
    }
}
apply plugin: 'com.google.gms.google-services
  • 注意使用即时通讯tools:node="replace"在我的根应用程序标签。 有没有可能是FirebaseInstanceIdService不添加到清单,因为它具有相同的意图过滤矿山FirebaseInstanceService ,因而不会被调用?

所以在这里,我的问题是有什么不对劲它的官方文档或在我的实现应该引起实例标记为空?

Answer 1:

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

为解决这个问题是增加这些类的应用程序清单manualy

 <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" />


Answer 2:

虽然我在做迁移我有一个问题,这是我没有收到任何通知。 为了解决这个问题,如果你有你的工作GCM,不要忘记将发件人ID,当你得到了火力地堡令牌。 请教这个ID,你必须导航到你的火力地堡项目,然后单击项目名称。 点击设置图标,选择“项目设置”菜单中选择“莫非信息功能”选项卡,并在网页上使用发件人ID。

当说,实现代码:

     token = FirebaseInstanceId.getInstance().getToken();

你必须使用这样的:

     token = FirebaseInstanceId.getInstance().getToken("YOUR_SENDER_ID", "FCM");

这对我的作品,我希望这可以帮助你;)



Answer 3:

首先genarate“谷歌services.json”,并在你的Android工作室加入这个文件中像这样projectFolder / APP /谷歌services.json

加入classpath中最高级别的build.gradle

 dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

添加以下的插件和依赖于应用程序的的build.gradle

apply plugin: 'com.google.gms.google-services'
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
}

添加在你的java文件下列火力地堡服务

FirebaseIDService.java

package com.galleonsoft.firebase.push;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class FirebaseIDService extends FirebaseInstanceIdService {
    private static final String TAG = "FirebaseIDService";

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);

        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }

    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

MyFirebaseMessagingService.java

package com.galleonsoft.firebase.push;

import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class MyFirebaseMEssagingService extends FirebaseMessagingService {
    private static final String TAG = "FCM Service";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO: Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    }
}

在AndroidMainifest.xml文件添加服务

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

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


文章来源: FirebaseInstanceIdService getToken returning null