【发布时间】:2017-07-31 00:45:38
【问题描述】:
我正在探索 firebase 云功能,并尝试使用 http 请求发送通知。
问题是即使我设法发送通知,请求总是超时。
这是我的脚本
/functions/index.jsconst functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.friendRequestNotification = functions.https.onRequest((req, res) => {
const senderId = req.query.senderId;
const recipientId = req.query.recipientId;
const getRecipientPromise = admin.database().ref(`/players/${recipientId}`).once('value');
const getSenderPromise = admin.database().ref(`/players/${senderId}`).once('value');
return Promise.all([getRecipientPromise, getSenderPromise]).then(results => {
const recipient = results[0];
const sender = results[1];
const recipientToken = recipient.child("notificationsInfo/fcmToken").val();
const notificationAuthorization = recipient.child("notificationsInfo/wantsToReceiveNotifications").val();
const recipientBadge = recipient.child("notificationsInfo/badgeNumber").val();
const senderUsername = sender.child("username").val();
const payload = {
notification: {
title: `FriendRequest`,
body: `You have a new friend request from ${senderUsername}!`,
badge: (recipientBadge+1).toString()
}
};
if (notificationAuthorization) {
return admin.messaging().sendToDevice(recipientToken, payload).then(response => {
});
}
return admin.database().ref(`/players/${recipientId}/notificationsInfo/badgeNumber`).setValue(recipientBadge+1);
});
});
另外,badgeNumber 好像一直没更新,是不是跟超时问题有关?
【问题讨论】:
-
对于可能遇到此线程的其他人,还请记住,向第三方发出的 HTTPS 请求需要您已支付 Firebase 帐户;您不能在免费帐户上向 Google 外部发送 HTTP 请求以防止滥用。
-
哦,我明白了,您从哪里找到这些信息的?数据库触发器呢?
-
在 Firebase 定价页面上,脚注 3 显示“Spark 计划仅允许向 Google 拥有的服务发出出站网络请求。在 Blaze 计划中,Cloud Functions 提供永久免费层。” firebase.google.com/pricing
标签: javascript node.js firebase firebase-cloud-messaging google-cloud-functions