java - FirebaseMessagingService onMessageReceived() 未执行
问题描述
我刚开始使用 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);
}
安卓清单
<?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)
接口:
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-ref 。https://firebase.google.com/docs/cloud-messaging/downstream。
推荐阅读
- javascript - 每个只返回第一个对象字符串的 Javascript JSON
- javascript - 如何在 JavaScript 中将 UUID/GUID 转换为 OID/DICOM UID?
- c++ - 派生类的切片是如何发生的?
- jmespath - 使用 JMESPath,根据数组值属性的长度过滤数组元素
- jar - weblogic wseeclient.jar 在从 weblogic 服务器迁移到 jboss 时,我想要一个用于 JBOSS 的替代 jar
- ruby - 在数组Ruby的第一个元素中创建逗号分隔的字符串
- ios - 键盘卡在 ios 13 模拟器中
- azure-devops - Azure DevOps Api - 获取剩余工作日
- javascript - 编辑表单时来自本地存储的复选框值
- ios - iOS 应用商店 NSBluetoothAlwaysUsageDescription - 拒绝