gpt4 book ai didi

javascript - 如何避免向 Firebase 的 'Miscellaneous' channel 发送通知?

转载 作者:行者123 更新时间:2023-12-04 10:36:39 24 4
gpt4 key购买 nike

我想在我创建的 channel 中显示我的通知,以便我可以根据我的偏好完全自定义我的 channel 。我正在使用 Firebase 函数在用户之间发送通知(消息):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.pushNotification = functions.firestore.document('/devices/{tokenId}/notifications/{notificationId}')
.onWrite((change, context) => {
console.log('Push notification event triggered');

const tokenId = context.params.tokenId;
const document = change.after.exists ? change.after.data() : null;

if (document == null) {
return console.log('A notification has been deleted from database');
}

const payload = {
notification: {
title: document.username,
body: document.message,
sound: "default"
},
data: {
sender: document.sender
}
};
const options = {
priority: "high",
timeToLive: 60 * 60 * 24 //24 hours
};

return admin.messaging().sendToDevice(tokenId, payload, options).then(result => {
console.log('A notification sent to device with tokenId: ', tokenId);
});
});

我已经实现了我的 FirebaseMessagingService 服务:
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
showNotification(remoteMessage);
}

private void showNotification(RemoteMessage remoteMessage) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
RemoteMessage.Notification remoteNotification = remoteMessage.getNotification();
if (remoteNotification == null) return;
String title = remoteNotification.getTitle();
String message = remoteNotification.getBody();

Notification notification;
Notification.Builder builder = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ?
new Notification.Builder(this, CH_MESSAGE) : new Notification.Builder(this);

notification = builder
.setContentTitle(title)
.setContentText(message)
.setCategory(CATEGORY_MESSAGE)
.build();

notificationManager.notify(0, notification);
}

并在我的 Application 类中创建了我自己的通知 channel :
@Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}

private void createNotificationChannels() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (notificationManager == null) return;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CH_MESSAGE,
getString(R.string.messages), NotificationManager.IMPORTANCE_HIGH);

notificationChannel.setDescription(getString(R.string.message_channel_description));

notificationManager.createNotificationChannel(notificationChannel);
}
}

我可以成功发送通知,但通知将转到杂项 channel 。

Screenshot

我试图通过使用 channel ID 和 notificationManager.deleteNotificationChannel("fcm_fallback_notification_channel"); 来删除 channel ,但它仍然重新创建 channel 并在那里发送通知。如何永久删除杂项 channel 并使用我自己的 channel 处理我的通知?

最佳答案

我发现了我面临的问题,我的消息有效负载包含通知和数据字段。根据 this documentation我的消息没有调用 onMessageReceived应用程序在后台时的方法。现在我只使用数据负载,当应用程序在后台和前台时调用该方法。

const payload = {
data: {
sender: document.sender,
username: document.username,
message: document.message
}
};

而在我的 onMessageReceived方法:
Map<String, String> remoteMap = remoteMessage.getData();
String senderUid = remoteMap.get("sender");
String senderUsername = remoteMap.get("username");
String message = remoteMap.get("message");

Notification.Builder builder = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O ?
new Notification.Builder(this, CH_MESSAGE) : new Notification.Builder(this);

Notification notification = builder
.setContentTitle(senderUsername)
.setContentText(message)
.setSmallIcon(R.drawable.ic_notif_message)
.build();

notificationManager.notify(0, notification);

关于javascript - 如何避免向 Firebase 的 'Miscellaneous' channel 发送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60149742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com