java - 无法通过 Firebase 接收消息 - Android

标签 java android firebase android-logcat

我正在使用此应用程序通过“firebase”接收消息。但没有收到消息。

检查以下代码:

FirebaseIdService.java

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);
    }

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
    }
}

MyFirebaseMessangingService.java

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());
    }
}

AndroidMenifest.java

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

build.gradle(应用程序)

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.firebase:firebase-core:9.2.0'
    compile 'com.google.firebase:firebase-messaging:9.2.0'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

build.gradle(项目)

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        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
    }
}

无论如何,“firebase”控制台表示有一个已注册的“firebase”服务。

最佳答案

您是否将 google-services.json 文件添加到项目的应用文件夹中?

关于java - 无法通过 Firebase 接收消息 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45811539/

相关文章:

JavaScript JQuery 为什么按钮的 ID 没有被正确引用

android - Firebase 云消息传递到底是什么?

android - Firebase android错误-java.util.zip.ZipException:重复的条目:com/google/android/gms/common/api/zzd.class

Java随机生成两位小数格式的价格

java - Spring 属性(property)整合

java - 如何使用Quarkus指定要在application.properties中使用的基本docker镜像

java - Android MediaPlayer seeBar 无法正常工作

java - 获取双 SIM 卡安卓手机的 SimOperatorName

java - "Maven clean"不起作用

android - Sqlite:根据距离(纬度和经度)选择项目