【问题标题】:FirebaseMessagingService onMessageReceived() not executingFirebaseMessagingService onMessageReceived() 未执行
【发布时间】:2019-09-23 09:29:12
【问题描述】:

我刚开始使用 Firebase Cloud Messaging,我在 Manifest 中设置了所有依赖项、Firebase 服务及其服务标签。

从控制台发送消息时,检测到目标用户,但应用没有收到通知。 所以我在 onMessageReceived() 上设置了一个断点。但它没有被点燃。

下面是我的firebase消息服务

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingServ";

@Override
public void onMessageReceived( RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String notificationBody = "";
    String notificationTitle = "";
    String notificationData = "";

    try{

        notificationData = remoteMessage.getData().toString();
        notificationTitle = remoteMessage.getNotification().getTitle();
        notificationBody = remoteMessage.getNotification().getBody();

    }
    catch (NullPointerException e){
        Log.e(TAG, "onMessageReceived: NullPointerException: " + e.getMessage() );

    }

    NotificationHelper.displayNotification(getApplicationContext(), notificationTitle, 
    notificationBody);
    Log.d(TAG, "onMessageReceived: data: " + notificationData);
    Log.d(TAG, "onMessageReceived: Notification body : " + notificationBody);
    Log.d(TAG, "onMessageReceived: notification title: " + notificationTitle);
}

Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.sys.systec">

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">



    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

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

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

</application>

【问题讨论】:

  • 你的消息依赖版本是什么?
  • 它是 'com.google.firebase:firebase-messaging:20.0.0'
  • 您必须检查您的通知是否有通知消息或数据消息....如果有通知消息,则 fcm 将处理通知,因此 onMessageReceived() 将不会执行...如果您创建一个只有数据消息的通知,那么您将完全控制 onMessageReceived() 方法
  • 如何检查是通知还是数据消息@unownsp
  • 如果您有一个发送通知的服务器,那么您可以检查它是如何创建通知的......如果您使用的是 firebase 控制台,那么它是通知消息@JoshM

标签: java android firebase-cloud-messaging


【解决方案1】:
if you are using firebase terminal you will face the issue so you can use notification API (use data message)

API:

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

标题:

"Content-Type": "application/json",
"Authorization": "key=<Server_key>"

正文:

{
    "to": "<Device FCM token>",
    "notification": {
      "title": "Demo tittle",
      "body": "Demo body",
      "mutable_content": true,
      },

   "data": {
    "url": "<media image url>",
    "dl": "<deeplink action>"
      }
}

参考:

网址:https://firebase.google.com/docs/cloud-messaging/http-server-refhttps://firebase.google.com/docs/cloud-messaging/downstream.

【讨论】:

  • 响应成功。但不显示来自 FirebaseMessagingService() 的通知或任何日志
  • 是的,我也遇到了这个问题,所以尝试使用 API 方法,您会收到通知。
猜你喜欢
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多