Kar*_*ård 2 javascript http-status-code-500 flutter google-cloud-functions firebase-cloud-messaging
我制作了一个 Flutter 应用程序,刚刚开始使用 Cloud Functions 向其他手机发送推送通知。我通过 API 调用来调用云函数,也就是说,消息被发送,我得到响应代码 200。但有趣的是,大约每隔一段时间(有时更多,有时更少)我得到回复:
状态代码:500,正文:“错误:无法处理请求”
这特别奇怪,因为目前,我每次都发送完全相同的消息!...正如,它是完全相同的 API 调用,没有标头和完全相同的正文。然而,我得到了不同的结果。问题可能是什么?
这是我的云功能:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports.sendMsg = functions.https.onRequest((request, response) => {
var decodedBody = JSON.parse(request.body);
const message = decodedBody;
// Send a message to the device corresponding to the provided
// registration token:
admin.initializeApp();
admin.messaging().send(message)
.then((res) => {
// Response is a message ID string.
console.log("Successfully sent message:", res);
response.send("Message sent!\n\n" + res);
})
.catch((error) => {
console.log("Error sending message:", error);
response.send("Error sending message:\n\n" + error);
});
});
Run Code Online (Sandbox Code Playgroud)
这是我的 API 调用:
import 'dart:convert';
import 'my_firebase.dart';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';
//...//
void msgFunction(){
final FirebaseMessaging _fcm = FirebaseMessaging();
await MyFirebase.myFutureFirebaseApp;
String fcmToken = await _fcm.getToken();
http.Response res;
try {
res = await http.post(
'https://us-central1-<my_project>.cloudfunctions.net/sendMsg',
body: jsonEncode({
"notification": {
"title": "This is my title",
"body": "Accept Ride Request",
},
"data": {
"score": "850",
"time": "245",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
},
"token": "$fcmToken",
}
),
);
} catch (e) {
print('Caught an error in API call!');
print('e is: ${e.toString()}');
if (res != null) print('Status code in apiCall() catch is ${res.statusCode}');
}
}
Run Code Online (Sandbox Code Playgroud)
执行失败的在线函数日志:
Function execution started
Function execution took 173 ms, finished with status: 'crash'
Run Code Online (Sandbox Code Playgroud)
解决了,我自己!
\n问题显然是admin.initializeApp();命令,我有点怀疑......因为在某些时候,日志告诉我这个函数被调用了两次。
解决方案是将其放在云函数之外,而不是放在云函数内部!像这样:
\nconst functions = require("firebase-functions");\nconst admin = require("firebase-admin");\nadmin.initializeApp();\n\nexports.sendMsg = functions.https.onRequest((request, response) => {\n\n var decodedBody = JSON.parse(request.body);\n const message = decodedBody;\n\n // Send a message to the device corresponding to the provided\n // registration token:\n admin.messaging().send(message)\n .then((res) => {\n // Response is a message ID string.\n console.log("Successfully sent message:", res);\n response.send("Message sent!\\n\\n" + res);\n })\n .catch((error) => {\n console.log("Error sending message:", error);\n response.send("Error sending message:\\n\\n" + error);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n这是视频号。Firebase Cloud Functions 视频系列教程中的第 2 部分让我明白了这一点:
\nhttps://firebase.google.com/docs/functions/video-series
\n这是一个很好的系列!我可以推荐它。
\n和平!\xe2\x9c\x8c\xef\xb8\x8f
\n| 归档时间: |
|
| 查看次数: |
1052 次 |
| 最近记录: |