gpt4 book ai didi

node.js - 使用适用于 iOS 应用的 Firebase、Node.js 和 Google Cloud Messaging 推送通知

转载 作者:太空宇宙 更新时间:2023-11-04 02:57:25 25 4
gpt4 key购买 nike

我正在尝试使用 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(响应):

{ multicast_id: 7398179070839209000,
success: 1,
failure: 0,
canonical_ids: 0,
results: [ { message_id: '0:1457564370785913%939a5f18939a5f18' } ] }

可能会发生什么情况?看起来事情应该可以正常工作,但事实并非如此

最佳答案

默认情况下,消息以标准优先级发送,根据 Google Cloud Messaging docs :

This is the default priority for message delivery. Normal priority messages won't open network connections on a sleeping device, and their delivery may be delayed to conserve battery. For less time-sensitive messages, such as notifications of new email or other data to sync, choose normal delivery priority.

不知怎的,这个正常的优先级似乎affect iOS more than Android apps .

要立即传递消息,您需要在消息中添加priority: 'high':

var message = new gcm.Message({
priority : "high",
notification: {
title: "Title",
body: notificationData.message
}
});

关于node.js - 使用适用于 iOS 应用的 Firebase、Node.js 和 Google Cloud Messaging 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35904572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com