【发布时间】:2020-08-05 20:24:59
【问题描述】:
我正在尝试通过 Flutter 发送 Firebase 推送通知。下面是代码
Future<Map<String, dynamic>> sendAndRetrieveMessage(String fcmToken, String title, String msg) async {
await _firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: false),
);
await http.post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: convert.jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{'body': msg, 'title': title},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': fcmToken,
},
),
).then((http.Response response){
final int statusCode = response.statusCode;
print("RESPONSE: " + response.body);
print("STATUS CODE: " + statusCode.toString());
if (statusCode < 200 || statusCode > 400 || response.body == null) {
throw new Exception("Error while fetching data");
}
});
}
以下是我得到的错误
RESPONSE: {"multicast_id":8498089791993785359,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}
令人惊讶的是,我可以通过 Firebase UI 控制台使用相同的 FCM 令牌来发送消息。它很简单。
更新
对于to 字段,如果我使用代码await _firebaseMessaging.getToken(),我不会收到错误并且它通过了。以上是用于获取同一用户的FCM,即string。
我知道我的其他用户的 fcm 令牌没有错误,因为在 Firebase 控制台中使用时,它可以工作。
这里有什么问题?
【问题讨论】:
-
你能分享一下错误吗
-
@LoVe:对不起,忘记了。我更新了帖子
-
这不是使用颤振,但它可能是同样的问题,请看看stackoverflow.com/q/42241432/9142279
-
@LoVe:我不认为这是问题所在。我正在使用服务器密钥
-
好的,抱歉重定向太多,但您是否也检查过这个?它可能包含可以解决您的问题的步骤,因为他们遇到了同样的错误github.com/flutter/flutter/issues/30486
标签: android firebase flutter push-notification firebase-cloud-messaging