【发布时间】:2021-05-09 20:30:46
【问题描述】:
我已经制作了一个 Flutter 应用程序,并且刚刚开始使用 Cloud Functions 向其他手机发送推送通知。我通过 API 调用调用 Cloud Function 来工作,也就是说,消息被发送,我得到响应代码 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);
});
});
这是我的 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}');
}
}
执行失败的在线函数日志:
Function execution started
Function execution took 173 ms, finished with status: 'crash'
【问题讨论】:
标签: javascript flutter google-cloud-functions firebase-cloud-messaging http-status-code-500