gpt4 book ai didi

javascript - Firestore 推送通知 "time out"错误通知并不总是被发送

转载 作者:行者123 更新时间:2023-12-04 11:32:30 25 4
gpt4 key购买 nike

我正在从我的应用程序中调用一个函数,该函数向应用程序上的特定用户发送通知。通知在大部分时间都成功发送,但很多时候都没有发送。当它没有被发送时,我检查日志以查看

Function execution took 60003 ms, finished with status: 'timeout'


我尝试过使用我的 promise /异步等待,但没有运气,因为我怀疑这就是问题所在。
这是我的云代码现在的样子
exports.sendNotification = functions.https.onRequest(async (request, response) => {

if (request.method !== "POST") {
response.status(400).send("Send it using post request");
return;
}

var toUid = request.body.toUid
var fcm = request.body.fcm
var fromUid = request.body.fromUid
var type = request.body.type
var fromName = request.body.fromName
var messageText = request.body.message

if (toUid === "" || fromUid === "" || fcm === "") {
response.status(400).send("Parameter is missing!");
return;
}

// common data for both platforms
const notification = {
title: fromName,
body: messageText,
}
const fcmToken = fcm

// ios specific headers
const apns = {
headers: {
"apns-collapse-id": 'toUid'
},
payload: {
aps: {
sound: 'default'
},
"data": {
"fromUid": fromUid,
"type": type
}
}
}

// final message
const message = {
token: fcmToken,
notification: notification,
apns: apns,
}

// send message
try {
return await admin.messaging().send(message);
response.status(200).send("Done");
} catch(e) {
console.log('Error sending message:', e);
}
});
我从应用程序调用函数如下
         AF.request("https://myproject.net/sendNotification", method: .post, parameters: parameters, encoding: JSONEncoding.default)
.responseString { response in
print(response)
DispatchQueue.main.async {
completion("done")
}
}
我已经看到其他类似问题的stackoverflow问题,建议使用 .post 和 JSONEncoding.default ,这就是我现在所拥有的。

最佳答案

来自 https://firebase.google.com/docs/functions/http-events#terminate_http_functions :
在你的 catch block 中没有调用 .send()或任何等价物,所以从上面的链接:

Always end an HTTP function with send(), redirect(), or end(). Otherwise, your function might continue to run and be forcibly terminated by the system. See also Sync, Async and Promises.


此外,最好将整个代码包装在 onRequest 中。在 try/catch 中回调。
这是带有建议修复的代码:
exports.sendNotification = functions.https.onRequest(async (request, response) => {

try {

if (request.method !== "POST") {
response.status(400).send("Send it using post request");
return;
}

var toUid = request.body.toUid
var fcm = request.body.fcm
var fromUid = request.body.fromUid
var type = request.body.type
var fromName = request.body.fromName
var messageText = request.body.message

if (toUid === "" || fromUid === "" || fcm === "") {
response.status(400).send("Parameter is missing!");
return;
}

// common data for both platforms
const notification = {
title: fromName,
body: messageText,
}
const fcmToken = fcm

// ios specific headers
const apns = {
headers: {
"apns-collapse-id": 'toUid'
},
payload: {
aps: {
sound: 'default'
},
"data": {
"fromUid": fromUid,
"type": type
}
}
}

// final message
const message = {
token: fcmToken,
notification: notification,
apns: apns,
}

// send message
await admin.messaging().send(message); // do not return here
response.status(200).send("Done");

} catch (e) {
response.status(500).send(e) // note the .send() wich terminates the request
}
});

关于javascript - Firestore 推送通知 "time out"错误通知并不总是被发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68199189/

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