我查阅了所有我能找到的答案,代码的特定功能甚至在前台都不起作用。我试着更改清单,更改代码,我在日志中得到的只有这两件事:
D/FA:记录事件(FE):通知接收(NR),…
这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
    android:allowBackup="true"
    android:icon="@drawable/avatar"
    android:label="@string/app_name"
    android:roundIcon="@drawable/avatar"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".StartActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"/>

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

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

</application>

这里是通知服务:
public class NotificationService extends FirebaseMessagingService {

public static  int NOTIFICATION_ID = 1;
private static final String CHANNEL_ID = "1";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d("+++", remoteMessage.toString());
    if (remoteMessage.getData().size() > 0) {
        Log.d("dataa", "Data Payload: " + remoteMessage.getData().toString());
    }

}

这是app gradle文件
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.xghos.Wrenchy"
    minSdkVersion 21
    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'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.3.0'
implementation 'com.ethanhua:skeleton:1.1.1'
implementation 'io.supercharge:shimmerlayout:2.1.0'
implementation "com.daimajia.swipelayout:library:1.2.0@aar"
}

configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
        if (!requested.name.startsWith("multidex")) {
            details.useVersion '26.1.0'
        }
    }
}
}

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

以及GettingDeviceTokenService:
package com.example.xghos.Wrenchy;

import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

public class GettingDeviceTokenService extends FirebaseMessagingService {

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("DeviceToken ==> ",  s);
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
}

}
编辑:通过将gettingdevicetokenservice的intent过滤器从
<action android:name="com.google.firebase.MESSAGING_EVENT" />


<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />

最佳答案

您应该考虑数据消息和显示消息之间的区别。
显示消息的有效负载键为notification,当应用程序在后台时自动显示,如果应用程序已经在前台,也会调用onMessageReceived()
数据消息具有payload keydata。它们总是调用onMessageReceived()

07-28 02:13