【问题标题】:Push Notifications using Firebase/Google Cloud Functions使用 Firebase/Google Cloud 函数推送通知
【发布时间】:2018-07-08 20:42:29
【问题描述】:

我正在尝试在将数字写入 _random 时发送通知。我能够获得设备令牌,并且云功能完美运行。但是,我没有在模拟器上收到通知。我正在使用推送到 Firebase 的通知令牌进行测试。如果有人可以提供帮助,将不胜感激。

https://i.stack.imgur.com/OB94c.png

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

//Initial function call:
exports.makeRandomFigures = functions.https.onRequest((req, res) => {
    //create database ref
    var rootRef = admin.database().ref();
    var doc_count_temp = 0;
    var keys = [];
    var random_num = 0;

    //get document count
    rootRef.once('value', (snapshot) => {
        doc_count_temp = snapshot.numChildren();
        //real number of member. if delete _timeStamp then minus 2 not 3!
        var doc_count = doc_count_temp - 3;

        //get num array previous generated
        var xRef = rootRef.child("_usedFigures");

        xRef.once('value', function(snap) {
            snap.forEach(function(item) {
                var itemVal = item.val();
                keys.push(itemVal);
            });
            //get non-duplicated random number
            var is_equal = true;
            while (is_equal) {
                random_num = Math.floor((Math.random() * doc_count) + 1);
                is_equal = keys.includes(random_num);
            }

            //insert new random vaule to _usedFigures collection
            rootRef.child('_usedFigures').push(random_num);
            rootRef.child('_random').set(random_num);
        });
    });

    //send back response 
    res.redirect(200);
});

exports.sendFigureNotification = functions.database.ref('_random').onWrite(event => {

    const payload = {
        notification: {
            title: 'Title',
            body: `Test`, //use _random to get figure at index key
            badge: '1',
            sound: 'default'
        }
    };
    
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24, //24 hours
        content_available: true
    };
  	const token = "cge0F9rUTLo:APA91bGNF3xXI-5uxrdj8BYqRPkxUPA5x9IQALtm3VEFJAdV2WQrQufNkzIclT5B671mBcvR6IDMbgSKyL7iG2jAuxRM3qR3MXhkNp1_utlXhCpE2VZqTw6Yw3d4iMMvHl1B-Cvik6NY";
    console.log('Sending notifications');
    return admin.messaging().sendToDevice(token, payload, options);

 
});

【问题讨论】:

  • 您没有使用您在 makeRandomFigures 中调用的任何 API 的承诺。事实证明,您正在任何异步工作完成之前发送响应(并终止函数)。这几乎肯定会导致问题。

标签: ios firebase firebase-cloud-messaging google-cloud-functions


【解决方案1】:

您无法在模拟器上收到推送通知。

要尝试一些替代方法,请查看此链接:

How can I test Apple Push Notification Service without an iPhone?

【讨论】:

  • 成功发送消息:{ 结果:[ { messageId: '0:1517287541355690%4448dfb34448dfb3' } ], canonicalRegistrationTokenCount: 0, failureCount: 0, successCount: 1, multicastId: 7812858106942759000 }
  • @EricDowdellII 这是一个firebase响应,它与模拟器或真实设备无关。确保您正确使用适当的功能在设备上获取和显示通知。
  • 所以发生了一些奇怪的事情。推送通知适用于我的项目合作伙伴的设备,但不适用于我的设备。代码也完全一样
  • 确保您的设备允许推送通知。尝试删除并重新安装应用程序。如果您使用 USB 安装,应该没问题。如果您使用无线安装,请确保将设备 udid 添加到配置文件中。或者最后,也许您可​​以尝试此链接。 stackoverflow.com/questions/48217757/…
  • 嘿,我想通了/我必须将 FirebaseAppDelegateProxyEnabled 更改为 YES...除非更改,否则它不会这样做
猜你喜欢
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2020-02-08
  • 2018-06-20
  • 2017-11-16
  • 2022-12-14
  • 2016-06-24
  • 2019-11-25
相关资源
最近更新 更多