java - FirebaseMessagingService onMessageReceived() 未执行

标签 java android firebase-cloud-messaging

我刚刚开始使用 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 list

<?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>

最佳答案

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 .

关于java - FirebaseMessagingService onMessageReceived() 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58059453/

相关文章:

java - 从输入流中读取实体时出错 - 使用 Jackson

java - 当我重新启动应用程序时,如何在 EditText 中保留 string/int 值?

android - Xamarin.android 默认 FirebaseApp 未在此过程中初始化

android - Firebase 云消息传递 : No Token and strange notification behavior

java - Java 中奇怪的日期解析错误

java - 带有 Set 初始化的通用方法

java - 如何从字符串中选择特定值并将其转换为整数

android - EventBus 是否适合从 RecyclerView 适配器到使用 RecyclerView 的 fragment 的通信

android - 我可以更改 ArCore 虚拟相机的姿势或在一个场景中有多个虚拟相机吗?

android - FCM 代币发行