我正在尝试在向_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);
});
无法在模拟器上接收推送通知。
要尝试一些替代方法,请检查此链接:
如何在没有 iPhone 的情况下测试 Apple 推送通知服务?