使用Firebase / Google Cloud功能推送通知

时间:2018-01-30 03:19:46

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

我正在尝试将数字写入_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);

 
});

1 个答案:

答案 0 :(得分:1)

您无法在模拟器上获得推送通知。

要尝试其他方法,请查看以下链接:

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