try {
var payload = {
// notification: {
// imageUrl: image.toString(),
// body: body.toString(),
// title: title.toString(),
// },
android: {
notification: {
imageUrl: image.toString(),
body: body.toString(),
title: title.toString(),
}
},
data: {
imageUrl: image.toString(),
body: body.toString(),
title: title.toString(),
},
token: tokens,
};
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: stage.databaseURL
}, '--------');
}
admin.apps.find(t => t.name == '---------').messaging().send(payload)
.then(function (response) {
resolve(response)
})
.catch(function (error) {
reject(error)
});
}
catch (e) {
reject(e)
}
这是我来自节点后端的有效负载
messaging().setBackgroundMessageHandler(async remoteMessage => {
// Handle the message, display a local notification
console.log('onMessage in the closed!');
if (remoteMessage.data || (remoteMessage.android && remoteMessage.android.notification)) {
const notificationData = remoteMessage.data || remoteMessage.android.notification;
PushNotification.localNotification({
channelId: "------",
message: notificationData.body,
title: notificationData.title,
largeIconUrl: notificationData.imageUrl,
when: Date.now(),
});
// return Promise.resolve();
}
// return Promise.resolve();
});
messaging().onMessage(async remoteMessage => {
console.log('onMessage in the background!', remoteMessage);
if (remoteMessage != null) {
FirebaseBackgroundTask(remoteMessage);
}
});
这是我在react-native中从前端接收的端
<service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="false" android:directBootAware="true">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="---------" />
这是我的 androidXMl.mainfest 文件
我试图在应用程序终止时获取自定义通知,但收到 2 个通知,一个由操作系统(默认)生成,另一个是我用图像自定义的自定义通知
默认的主要问题是没有生成图像或者不是我想要的方式(获取大图片而不是图标)
Please below steps when your app is terminated or in the background, handling Firebase Cloud Messageing.
1.Update AndroidManifest.xml
<service
android:name=".YourFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
2.Implement a FirebaseMessagingService
public class YourFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Handle the message payload here
// You can check if the app is in the foreground, background, or terminated
if (remoteMessage.getData().size() > 0) {
// Handle data payload
// You can extract values from remoteMessage.getData()
}
// Check if the message contains a notification payload.
if (remoteMessage.getNotification() != null) {
// Handle notification payload
}
}
}
3.Handle Notification Payload in the on MessageReceived
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
// Handle data payload
// You can extract values from remoteMessage.getData()
}
// Check if the message contains a notification payload.
if (remoteMessage.getNotification() != null) {
// Handle notification payload
}
}
4.Check App State
if (isAppInForeground()) {
// App is in the foreground, handle the notification without showing a system notification
} else {
// App is in the background or terminated, show a notification in the system tray
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
导致此问题的可能原因有多种。您可以采取以下一些步骤来排查和解决问题:
检查您的代码: 查看处理推送通知的代码。确保您没有无意中创建和发送两个单独的通知。
Firebase 控制台配置: 在 Firebase 控制台上仔细检查您的 Firebase 项目设置。
通知负载: 检查您发送的推送通知的有效负载。确保您没有同时包含默认通知字段和自定义通知字段。
在前台处理通知: 确保您处理得当。
检查重复注册: 确保您的应用没有多次注册推送通知。
更新 Firebase SDK
7.检查 Firebase 文档