android - Fire base onMessageReceived 根本不起作用

标签 android firebase push-notification firebase-cloud-messaging

我查看了我能找到的所有答案,代码的特定功能甚至在前台也不起作用。我尝试更改 list ,更改代码,我在记录器中得到的只是其中的两个: D/FA:记录事件(FE):notification_receive(_nr), ...

这是我的 list 文件:

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

}

这是应用程序的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()

数据消息有有效载荷键 data 。他们总是调用 onMessageReceived()

关于android - Fire base onMessageReceived 根本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51885902/

相关文章:

android - Titanium Appcelerator 可以用于 Windows 机器中的 IOS 应用程序开发吗

android - 使用 Firebase UI 依赖项的重复条目 Gradle 错误

firebase - Flutter Cloud Firestore在调试时仅获取最新数据

java - 调用接口(interface)方法而不指定实现接口(interface)的类

php - 对数据库 MySql 中的所有 token 发送推送通知

android - 错误 :(2, 0) 找不到 id 为 'com.github.dcendents.android-maven' 的插件

android - 如何给整个布局半透明(半透明)的外观,并在点击 Android 工具栏中的图标后使其再次看起来正常?

javascript - 云功能执行成功但用户没有收到通知?

ios - 推送通知不适用于 ios 8.3?

android - 如何使 GCM 与应用程序和服务器保持同步?