使用Firebase FCM时,为什么不能收起推送通知? [英] Why can't I collapse my push notifications when I use Firebase FCM?

查看:454
本文介绍了使用Firebase FCM时,为什么不能收起推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const options = {
    priority: 'high',
    collapseKey: user_id
};
const deviceTokensPromise = db.ref('/users-fcm-tokens/' + user_id).once('value');
deviceTokensPromise.then(tokensSnapshot => {
    if (!tokensSnapshot.hasChildren()) {
        return console.log('There are no device tokens to send to.');
    }
    const tokens = Object.keys(tokensSnapshot.val());
    console.log(tokens);
    console.log(payload);
     return admin.messaging().sendToDevice(tokens, payload, options).then(response => {
         console.log(response);
         return removeInvalidFCMTokens(tokensSnapshot, response);
     });
});

我的选项中有一个折叠键字段.

I have a collapse-Key field in my options.

运行此代码后,iPhone会收到多个通知,且所有通知都相互叠加.我希望最近的通知取代以前的通知.

When this code is ran, the iPhone receives multiple notifications, all on top of each other. I'd like to have most recent notification replace the previous ones.

推荐答案

在Android上通过FCM的collapse_key支持

可折叠"消息行为,在iOS上通过apns-collapse-id以及在JavaScript/Web上通过Topic支持

"collapsible" message behavior is supported on Android via FCM's collapse_key, on iOS via apns-collapse-id, and on JavaScript/Web via Topic.

直观上,您可能希望apns-collapse-id设置可能会传递到您正在使用的sendToMessage方法中传递的options参数中.然而,这种情况并非如此.而是尝试将其修补到payload对象中,如下所示:

Intuitively you might expect that the apns-collapse-id setting might go into the options parameter passed into the sendToMessage method you are using. However, this is not the case. Instead try patching it into the payload object, like this:

const patchedPayload = Object.assign({}, payload, {
    apns: {
        headers: {
            'apns-collapse-id': user_id
        }
    }
});

这遵循上面链接的文档中提供的payload格式.

This follows the payload format presented in the documentation linked above.

一旦构建了此修补的有效负载,别忘了将sendToDevice(tokens, payload, options)更新为sendToDevice(tokens, patchedPayload, options).

Once you've constructed this patched payload don't forget to update sendToDevice(tokens, payload, options) to sendToDevice(tokens, patchedPayload, options).

希望这对您有用!

这篇关于使用Firebase FCM时,为什么不能收起推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆