Firebase云消息传递仅间歇性地适用于IOS

时间:2018-11-01 00:25:50

标签: ios typescript firebase firebase-cloud-messaging

尽管我终于使推送通知生效,但似乎结果不一致,有时即使firebase日志显示功能已成功完成,我的手机仍会在后台收到推送通知,有时什么也没有。这是代码,我添加了优先级,但结果仍然相同。

exports.notificationForCommunicationAdded = functions.database.ref(`/{pharmacyId}/orders/{orderId}/communications`)
.onWrite((change, context) => {

    const pharmacyId = context.params.pharmacyId;

    var tokensArray = [];

    var payload = {
        notification: {
            title: "Communication edited",
            body: "Finally got it working"
        }
    };

    var options = {
        priority: "high",
        timeToLive: 60 * 60
    };

    admin.database().ref(`/` + pharmacyId + `/userDevices`).once('value').then((snapshot) => {
        snapshot.forEach(function(childSnapshot) {
            var item = childSnapshot.val();
            tokensArray.push(item.fcmToken)
            console.log(item.fcmToken)
        })
        return admin.messaging().sendToDevice(tokensArray, payload, options);
    }).catch((error) => {
        console.log(error)
        return null
    });

});

我记录了从数据库中获取令牌的过程,以检查它是否已成功完成,即使通知没有出现在手机上,它也始终返回设备令牌。

0 个答案:

没有答案