【发布时间】:2016-06-24 14:15:25
【问题描述】:
我正在尝试使用 Firebase、Node.js 和 Google Cloud Messaging 为 iOS 应用发送推送通知。下面是 Node.js 代码
var Firebase = require('firebase'),
gcm = require('node-gcm')
;
var pushRef = new Firebase("https://<myapp>.firebaseio.com/ios/queue/tasks");
pushRef.on("child_added", function(snapshot) {
console.log("child added event registers");
var notificationData = snapshot.val();
sendNotification(notificationData);
snapshot.ref().remove();
});
function sendNotification(notificationData) {
var message = new gcm.Message({
notification: {
title: "Title",
body: notificationData.message
}
});
var sender = new gcm.Sender(<my Google Server Key>);
var registrationTokens = [notificationData.recipientId];
console.log("about to send message");
console.log("this is the registration token: " + registrationTokens);
sender.send(message, { registrationTokens: registrationTokens }, 10, function (err, response) {
if(err) {
console.error(err);
} else {
console.log(response);
}
});
}
没有错误,所有 console.log 语句都已注册。但是,我从未在我的设备上收到推送通知。这是console.log(response):
{ multicast_id: 7398179070839209000,
success: 1,
failure: 0,
canonical_ids: 0,
results: [ { message_id: '0:1457564370785913%939a5f18939a5f18' } ] }
可能会发生什么?看起来事情应该可以正常工作,但事实并非如此
【问题讨论】:
-
您可以在您的消息中添加
priority: 'high'吗?我们最近遇到了一个问题,需要向 iOS 设备发送通知(除非该应用已经处于活动状态)。
标签: node.js push-notification firebase google-cloud-messaging