我们使用 https://firebase.google.com/docs/cloud-messaging/send-message 中的以下实现通过 Firebase Cloud Messaging 发送消息,但在 FCM-PROD
环境中,我们收到以下超时异常:
2021-05-06T12:12:24.242Z 7da528c7-b898-4b0b-9624-c4b6e26bdf58 INFO 2021-05-06T12:12:24.242Z [DEBUG] firebasePushNotification >> [DEBUG] - [firebaseMessenger.sendMulticast] - Start :: "2021-05-06T12:12:24.242Z"
END RequestId: 7da528c7-b898-4b0b-9624-c4b6e26bdf58
REPORT RequestId: 7da528c7-b898-4b0b-9624-c4b6e26bdf58 Duration: 20020.44 ms Billed Duration: 20000 ms Memory Size: 128 MB Max Memory Used: 50 MB
2021-05-06T12:12:39.485Z 7da528c7-b898-4b0b-9624-c4b6e26bdf58 Task timed out after 20.02 seconds
是什么导致了这个超时以及如何避免这个错误?
这是我们使用的代码:
const admin = require("firebase-admin");
const db = require('../lib/dbHelper');
const config = require("../config");
// ### export
var firebase = {};
module.exports = firebase;
// ### config
var firebaseCreds = config.FIREBASE_SERVICE_ACCOUNT;
admin.initializeApp({
credential: admin.credential.cert(firebaseCreds)
});
const firebaseMessenger = admin.messaging();
firebase.sendPushNotificationToDevices = async function (tokenList, text, data, title, category) {
if (category) {
data.category = category;
}
var payload = {
tokens: tokenList,
"notification": {
"title": title,
"body": text
},
data: data,
android: {
notification: {
click_action: "SplashActivity"
}
},
apns: {
payload: {
aps: {
category: category
}
}
},
content_available: true,
mutable_content: true
};
console.log("[Integration Testing] - Entire FCM payload :: %j", payload);
try {
// sends pushNotification to registered devices using Firebase's default messaging app
console.log("[DEBUG] - firebaseMessenger.sendMulticast - Start :: %j", new Date().toISOString());
const response = await firebaseMessenger.sendMulticast(payload);
console.log("[DEBUG] - firebaseMessenger.sendMulticast - End :: %j", new Date().toISOString());
return {
statusCode: 202,
headers: {'content-type': 'application/json'},
body: JSON.stringify(response)
};
} catch (err) {
console.log("[sendPushNotificationToDevices] Error Occured due to :: %j", err);
return {
statusCode: 400,
headers: {'content-type': 'application/json'},
body: JSON.stringify(err.description || err)
};
}
};