android - 无法解析 FirebaseMessagingService android

标签 android firebase gradle

我是 android studio 的新手,我正在尝试设置 firebase 消息传递,但我不断在消息接收器类中收到错误:

cannot resolve FirebaseMessagingService

我已经设置了所有的 gradle 依赖项,不确定哪里出错了,这是我的代码:

项目 Gradle 脚本

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

应用程序gradle脚本

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.app.com"
        minSdkVersion 22
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    allprojects {
        repositories {
            jcenter()
            maven {
                url "https://maven.google.com"
            }
        }
    }
}

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:27.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.google.firebase:firebase-messaging:11.8.0'
    compile 'com.google.android.gms:play-services:11.8.0'
    testCompile 'junit:junit:4.12'
}

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

我的类(class):

package com.app.com;

import FirebaseMessagingService;

/**
 * Created by abdulahmad on 1/4/18.
 */

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

最佳答案

显然您的导入错误,From docs应该是

import com.google.firebase.messaging.FirebaseMessagingService;

而不是

import FirebaseMessagingService;

关于android - 无法解析 FirebaseMessagingService android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48099864/

相关文章:

Android GPS 不显示或写入文件

java - Android Firebase 使用地理定位来过滤数据

ios - 什么是 iOS 版本的 firebase 列表适配器?

android - 如何使用 gradle 将 org.apache.commons.lang3 添加到 Android Studio

android - 如何使用布局膨胀一个 View

java - 从 firebase 数据库检索数据并将其存储在数组列表中

android - 我的 Android 应用程序的包名/域名重要吗?

android - 未创建任何 Firebase 应用 - 调用 Firebase.initializeApp

android - Gradle构建错误解决依赖项

安卓工作室 2.1.1 : "no usb devices or running emulators detected"