Firebase onMessageReceived 未调用

Firebase onMessageReceived not called(Firebase onMessageReceived 未调用)
本文介绍了Firebase onMessageReceived 未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FCM 服务:

公共类 FCMService 扩展 FirebaseMessagingService {@覆盖公共无效 onMessageReceived(RemoteMessage remoteMessage) {Log.i(Constants.TAG,"onMessageReceived");Log.i(Constants.TAG, "From: " + remoteMessage.getFrom());if (remoteMessage.getData().size() > 0) {Log.d(Constants.TAG, "消息数据负载:" + remoteMessage.getData());}if (remoteMessage.getNotification() != null) {Log.d(Constants.TAG, "消息通知正文:" + remoteMessage.getNotification().getBody());}}}

应用分级:

 编译 'com.google.firebase:firebase-messaging:10.2.6'

清单:

 <service android:name="communications.FCMService"><意图过滤器><action android:name="com.google.firebase.MESSAGING_EVENT"/></意图过滤器></服务>

-我从 FCM 控制台发送消息

从控制台创建的消息将始终是通知消息!

首先,通过控制台直接向您的设备(使用 fcm 令牌)并添加带有示例值的简单数据有效负载.现在检查您的服务 onMessageReceived 是否被调用.(您的应用必须在前台!)

后端

使用 REST API,您可以发送仅包含自定义键值对且始终到达您的服务的数据消息!

您的后端应该自己编写消息并添加数据负载.这在此处有所描述:HTTP 协议数据有效负载

您还可以通过以下几种方式之一测试后端明智的发送消息您在网上找到的 http 请求生成器.我喜欢:hurl.it 和

邮寄到:

<块引用>

https://fcm.googleapis.com/fcm/send

4.摇篮

com.google.firebase:firebase-messaging:20.0.0 <- 使用这个,或更高版本

也就是说,低于该版本的版本存在错误 '11.0.4'.

5.链接和良好的阅读

进一步阅读这里的文档:接收消息文档以及作为 SOF 答案的消息类型的一个很好的解释:SOF - 回答 - 消息类型

FCM service:

public class FCMService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        Log.i(Constants.TAG,"onMessageReceived");

        Log.i(Constants.TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage.getData().size() > 0) {
            Log.d(Constants.TAG, "Message data payload: " + remoteMessage.getData());
        }


        if (remoteMessage.getNotification() != null) {
            Log.d(Constants.TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        }


    }

}

App Gradle:

  compile 'com.google.firebase:firebase-messaging:10.2.6'

Manifest:

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

-I send message from FCM console and don´t arrives. FCM console show me OK when message is sent.

-I send message from java server and message don´t arrives. Java server show me OK when message is sent.

-To send messages I use generated token in Android device.

-Before, I had a mistake to import the FCM library badly, that error is solved. When that past error occurred every time a message was sent, an exception occurred on Android. Now nothing is received.

-I have on the Android device an Internet connection and I receive messages via GCM (not FCM) from another application that I implemented in the past.

-App is in foreground.

Some log with "fcm" filter. I get this log when I send message.

08-14 12:09:25.640 800-4739/? D/PowerManagerService: acquireWakeLockInternal: lock=862109980, flags=0x1, tag="wake:bidsy.app.bidsy/communications.FCMService", ws=null, uid=10354, pid=24073
08-14 12:09:25.640 800-4739/? D/PowerManagerNotifier: onWakeLockAcquired: flags=1, tag="wake:bidsy.app.bidsy/communications.FCMService", packageName=bidsy.app.bidsy, ownerUid=10354, ownerPid=24073, workSource=null
08-14 12:09:25.683 24073-24073/bidsy.app.bidsy D/ActivityThread: SVC-Creating service: CreateServiceData{token=android.os.BinderProxy@1c269013 className=communications.FCMService packageName=bidsy.app.bidsy intent=null}
08-14 12:09:25.683 24073-24073/bidsy.app.bidsy D/ActivityThread: SVC-CREATE_SERVICE handled : 0 / CreateServiceData{token=android.os.BinderProxy@1c269013 className=communications.FCMService packageName=bidsy.app.bidsy intent=null}
08-14 12:09:25.685 24073-24073/bidsy.app.bidsy D/ActivityThread: SVC-SERVICE_ARGS handled : 0 / ServiceArgsData{token=android.os.BinderProxy@1c269013 startId=1 args=Intent { act=com.google.firebase.MESSAGING_EVENT pkg=bidsy.app.bidsy cmp=bidsy.app.bidsy/communications.FCMService (has extras) }}
08-14 12:09:25.684 24073-24073/bidsy.app.bidsy D/ActivityThread: SVC-Calling onStartCommand: communications.FCMService@3e035250, flags=0, startId=1
08-14 12:09:25.691 800-1592/? D/PowerManagerService: releaseWakeLockInternal: lock=862109980 [wake:bidsy.app.bidsy/communications.FCMService], flags=0x0, total_time=52ms
08-14 12:09:25.691 800-1592/? D/PowerManagerNotifier: onWakeLockReleased: flags=1, tag="wake:bidsy.app.bidsy/communications.FCMService", packageName=bidsy.app.bidsy, ownerUid=10354, ownerPid=24073, workSource=null
08-14 12:09:25.692 24073-24073/bidsy.app.bidsy D/ActivityThread: SVC-Destroying service: communications.FCMService@3e035250

It seems like the message arrives at the service but does not show it.

Of course I do not use any filters in the log to check if the message gets to onMessageReceived

I find similar question here not answered, has same log:

Can't receive the push notification message

After looking at the whole log in detail I have seen that when I send a message the following Firebase error occurs:

D / FirebaseMessaging: Unknown intent action: com.google.firebase.MESSAGING_EVENT

Solution to my problem is here:

I faced a issue while using the older version of FCM i.e 10.2.6 Unknown intent action: com.google.firebase.MESSAGING_EVENT

解决方案

1. tl;dr

Your messages, that are send from somewhere, need to have a data payload and no notification payload, or they won't arrive consistently inside of "onMessageReceived" in your MessagingService.

Notification messages contain a predefined set of user-visible keys. Data messages, by contrast, contain only your user-defined custom key-value pairs. Notification messages can contain an optional data payload.

("Notification messages can contain an optional data payload", this will only get submitted to your service, if your application is in foreground!)

  • Messages created from the console will always be notification-messages!

  • You can't send plain data-messages, that will consistently arrive at your service through the firebase-console. This is only possible via the REST-API (FCM protocols) or the Admin SDK.

One more important thing:

If you send Notification Messages with a data payload, that you wish to handle, you must handle not only it via the service if the app is in foreground, but also gathering the data from the Intent that your Launch-Activity will receive, which will be opened, if the user clicks the notification! The data payload will be inside of the intent!


2. Types of messages:

Notification Messages and Data Messages.

  1. Notification messages contain a predefined set of user-visible keys. Notification messages have an optional notification payload and will be delivered to the users system tray.

    • If the app is in foreground, they will call onMessageReceived, if the notification has data payload.

    • If the application is background, the notification will be displayed and the data payload is given through the intent of the launching activity.

    • If the notification has no optional data payload, the notification will be displayed and the application is opened when clicked.

  2. Data Messages contain only your user-defined custom key-value pairs. (explained below). And if you want that the message consistently call the onMessageReceived, they should not contain a notification payload, but a data payload.

From the docs:

Client app is responsible for processing data messages. Data messages have only custom key-value pairs. (! once again - only custom key-value pairs)

You can read about the message types here: Fcm Message Types


3. Data Payload

The data payload is a simple json entry, additionally in your payload and can have simple key, value pairs.

Example:

  "data": {
        "eventId" : "1",
        "flavors" : "alpha",
        "minFcmVersion" : "3",
        "showFallbackOnLowVersion" : "false"
  }

Inside of the console, you can add the data payload, if you enter something below the "extended options" field:

Messages created from the console will always be notification-messages!

At first, send a firebase cloud message via the console, directly to your device (use the fcm token) and add a simple data-payload with example values. Now check if your services onMessageReceived is called. (You app must be in foreground!)

Backend

With the REST API you can send Data-Messages, that only contain custom key-value pairs and will always arrive to your service!

Your backend should compose the message itself and add a data payload. This is somewhat described here: HTTP protocol data payload

You can also test a backend wise sent message via one of the several http request builder you find online. I like: hurl.it and

Post to:

https://fcm.googleapis.com/fcm/send

4. Gradle

com.google.firebase:firebase-messaging:20.0.0 <- use this, or higher

That said, there was a bug in versions lower then '11.0.4'.

5. Links and good read

Further good to read documentation here: Receive Messages Documentation And a good explanation on message types as an SOF Answer: SOF - Answer - Message Types

这篇关于Firebase onMessageReceived 未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)