【问题标题】:Firebase Admin SDK sendToTopic not workingFirebase Admin SDK sendToTopic 不起作用
【发布时间】:2018-05-02 06:17:10
【问题描述】:

我正在部署一个移动应用程序(适用于 Android 和 iOS),管理员可以通过它向注册到特定主题的用户发送警报。为此,我使用实时数据库来存储警报和云功能以向主题发送通知。

我部署了以下云功能:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNewAlertNotification = functions.database.ref('/alerts').onWrite(event => {
    const getValuePromise = admin.database()
        .ref('alerts')
        .orderByKey()
        .limitToLast(1)
        .once('value');

    return getValuePromise.then(snapshot => {
        const { text, topics, author } = snapshotToArray(snapshot)[0];

        const payload = {
            data: {
                title: 'Avviso',
                body: text,
                icon: 'ic_stat_notify',
                sound: 'default',
                color: '#F3E03B',
                tag: 'alerts',
                ticker: 'Nuovo avviso',
                subtitle: 'Avvisi',
                author: JSON.stringify(author)
            }
        };

        const options = {
            priority: 'high',
            timeToLive: 60 * 60 * 24 * 2, // 48 hours
            collapseKey: 'it.bmsoftware.caliup'
            // contentAvailable: true
        };

        if (topics.length > 1) {
            let condition = '';
            topics.forEach((topic, index) => {
                condition += `'${topic}' in topics`

                if (index < topics.length - 1) {
                    condition += ' || '
                }
            });
            console.log(`Sending alert to condition '${condition}' -> ${JSON.stringify(payload)}`);
            return admin.messaging().sendToCondition(condition, payload, options);
        } else if (topics.length === 1) {
            let topic = topics[0];
            console.log(`Sending alert to topic '${topic}' -> ${JSON.stringify(payload)}`);
            return admin.messaging().sendToTopic(topic, payload, options);
        } else {
            console.log(`No topics found`);
        }
    });
});

const snapshotToArray = (snapshot) => {
    let result = []
    if (!snapshot || !snapshot.val())
        return result

    snapshot.forEach((childSnapshot) => {
        let item = childSnapshot.val()
        item.key = childSnapshot.key
        result.push(item)
    })

    return result
}

当我在实时数据库中插入一条新消息时,上述函数会正确获取该消息,并且在日志部分(在 firebase 控制台上)我会看到正确的自定义日志和一个显示 status 'ok' 的日志。

尽管如此,设备上没有收到任何通知。如果我直接从 firebase 控制台测试相同的主题,它可以正常工作,因此设备已正确注册。

我缺少的云功能有什么问题吗?

【问题讨论】:

    标签: android ios firebase-realtime-database firebase-cloud-messaging firebase-admin


    【解决方案1】:

    如果您只发送 data 有效负载,我认为您应该取消注释 // contentAvailable: true,至少对于 iOS。这样您就可以在应用程序代码上自己显示和触发通知。如果您希望在不处理数据负载的情况下弹出通知,您应该在负载上传递一个notification 对象。

    通知仅限于以下字段:https://firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 1970-01-01
      • 2019-06-23
      • 2018-05-02
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2013-10-05
      • 2019-10-23
      相关资源
      最近更新 更多