Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – FirebaseInstanceIdService getToken返回null大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将FCM通知添加到我的应用程序,但由于某种原因

FirebaseInstancEID.geTinstance().getToken();

返回null.

由于我没有得到任何堆栈跟踪,我最好的猜测是FirebaseInstancEIDservice不起作用

> Google Play服务版:9.6.1
> Firebase消息传递版本:9.6.1
>从FirebaseConsole添加了json配置文件(在其中包含2个客户端的尝试单个文件,一个用于调试,一个用于发布版本)和两个文件用于各自的构建

我在我的模块gradle脚本底部应用了google服务插件
在项目根gradle脚本中包含谷歌服务

从officail docs创建了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://scheR_210_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_210_11845@as.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>

root Gradle:

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'
}

以及Gradle模块的主要部分

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

>请注意我在我的根应用程序标记中使用工具:node =“replace”.
FirebaseInstancEIDservice是否可能未添加到清单中,因为它具有与FirebaseInstanceservice相同的intent过滤器,因此不会被调用

所以我的问题是,官方文档或我的实现中是否存在错误导致实例令牌为空?

解决方法

@H_197_50@ 通过挖掘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" />

大佬总结

以上是大佬教程为你收集整理的android – FirebaseInstanceIdService getToken返回null全部内容,希望文章能够帮你解决android – FirebaseInstanceIdService getToken返回null所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。