火灾基础 onMessageReceived 根本不起作用

Posted

技术标签:

【中文标题】火灾基础 onMessageReceived 根本不起作用【英文标题】:Fire base onMessageReceived isn't working at all 【发布时间】:2018-08-16 22:03:21 【问题描述】:

我查看了所有我能找到的答案,代码的特定功能即使在前台也无法正常工作。我尝试更改清单,更改代码,我在记录器中得到的只是以下两件事: D/FA:记录事件 (FE):notification_receive(_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());
    


这是应用程序的 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 的意图过滤器解决了问题
<action android:name="com.google.firebase.MESSAGING_EVENT" />

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

【问题讨论】:

查看这个答案***.com/a/40458361/6559031 现在无关紧要,我还没有尝试获取数据或通知有效负载,我只是尝试记录远程消息并且它没有记录它。该应用程序在前台,我不明白为什么它根本不工作,如果应用程序在前台通知和数据有效负载,它应该调用 onMessageReceived 发布你的 gradle 文件.. 你的 GettingTokenService() 是什么。因为您已经将 GettingTokenServiceNotification Service 声明为 MESSAGE_EVENTS 你的GettingTokenService() 必须有意图过滤器&lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT" /&gt; 终于成功了,问题真的是我用MESSAGING_EVENT的intent filter声明了2个服务,谢谢大家的帮助! 【参考方案1】:

您应该考虑数据消息和显示消息之间的区别。

显示消息具有带有密钥notification 的有效负载,并且在应用程序处于后台时自动显示,如果应用程序已经在前台,也会调用onMessageReceived()

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

【讨论】:

我至少现在正试图让它在前台工作,这两个日志甚至没有显示,如果它与通知中的通知/数据密钥有效负载有关,我可以'甚至看不到我在 onMessageReceived 中提供的日志,因此在收到消息时甚至不会调用该函数 如果您从firebase发送数据消息,无论应用程序是在前台还是后台,都会调用该函数【参考方案2】:

您已在清单中注册NotificationService 服务。由于 firebase 通知的最新更新,相同的服务将处理新的令牌和收到的消息。在您的情况下,您应该只在清单中注册GettingDeviceTokenService,因为您覆盖了这两种方法并删除了NotificationService

【讨论】:

【参考方案3】:

试试这个方法。它非常适合我。 1. 创建服务

public class YourService extends FirebaseMessagingService 

public static int NOTIFICATION_ID = 1;

@Override
public void onNewToken(String s) 
    super.onNewToken(s);


@Override
public void onMessageReceived(RemoteMessage remoteMessage) 
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(this, "2")
            .setSmallIcon(R.drawable.your_icon)
            .setContentTitle(remoteMessage.getNotification().getTitle())
            .setContentText(remoteMessage.getNotification().getBody())
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (NOTIFICATION_ID > 1073741824) 
        NOTIFICATION_ID = 0;
    
    Objects.requireNonNull(notificationManager).notify(NOTIFICATION_ID++, mNotifyBuilder.build());

现在将其添加到清单

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

【讨论】:

我有单独的函数来处理通知,但我还没有使用它们,因为我什至无法让我在那里显示的 2 个日志工作。另外,如您所见,我已在清单中声明了它。

以上是关于火灾基础 onMessageReceived 根本不起作用的主要内容,如果未能解决你的问题,请参考以下文章

火灾检测基于matlab GUI森林火灾检测系统(带面板)含Matlab源码 1921期

Firebase 云消息传递 onMessageReceived 未触发

未调用 HmsMessageService onMessageReceived

超市火灾烟气蔓延及人员疏散的matlab仿真模拟

Android FCM - onMessageReceived 未在后台调用

onMessageReceived(RemoteMessage remoteMessage) 应用程序在后台或被杀死时不调用