【问题标题】:How to avoid sending notifications to 'Miscellaneous' channel at Firebase?如何避免向 Firebase 的“杂项”频道发送通知?
【发布时间】:2020-05-25 17:34:52
【问题描述】:

我想在我创建的频道中显示我的通知,以便我可以根据自己的喜好完全自定义我的频道。我正在使用 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);
}

并在我的应用程序类中创建了我自己的通知通道:

@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);
    }
}

我可以成功发送通知,但通知会转到杂项频道。

我尝试通过将频道 ID 与 notificationManager.deleteNotificationChannel("fcm_fallback_notification_channel"); 一起使用来删除频道,但它仍会重新创建频道并在那里发送通知。如何永久删除杂项频道并使用自己的频道处理通知?

【问题讨论】:

  • 回答问题的标题:“如何避免向 Firebase 的 'Miscellaneous' 频道发送通知?” 为 FCM 消息设置默认频道,它们将发送到那里而不是miscellaneous

标签: javascript google-cloud-functions firebase-cloud-messaging android-notifications firebase-admin


【解决方案1】:

我发现了我面临的问题,我的消息有效负载包含通知和数据字段。根据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);

【讨论】:

  • 我认为从技术上讲,“正确”的解决方案是从 Intent Extras 中读取数据,但就像您所做的那样,避免实现更多逻辑来读取 Intent Extras 似乎更好。
猜你喜欢
  • 1970-01-01
  • 2015-06-02
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 2018-11-23
相关资源
最近更新 更多