我使用 firebase_messaging
进行推送通知并使用 POST
请求发送消息。
当向FCM token
发送消息时,一切正常,但是当将它们发送到topic
时(我基本上修改了用于token
的请求> 消息)它们不会被发送。
我检查过,主题是正确的,并且在物理 Android 设备上我正确地订阅了该主题,因为从 FCM 控制台发送主题消息时,它会立即传递。 你能发现我在这里做错了什么吗?
非常感谢。
token 消息(工作):
void sendOrderCollected(Order order) async {
var customerName = order.customerName;
var customerFcmToken = order.customerFcmToken;
await post('https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$firebaseServerKey'
},
body: jsonEncode({
'notification': <String, dynamic>{
'title': sprintf(
AppLocalizations.instance.text('ORDER_COLLECTED_PUSH_SUBTITLE'),
[order.shopName]),
'body': sprintf(
AppLocalizations.instance.text('ORDER_COLLECTED_PUSH_BODY'),
[customerName]),
'sound': 'true'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': customerFcmToken
})).whenComplete(() {
// print('sendOrderCollected(): message sent');
}).catchError((e) {
print('sendOrderCollected() error: $e');
});
}
主题消息(未发送):
void sendNewPromotion(Promotion promotion,String topic) async {
print('sendNewPromotion() web started.\n topic: $topic, promotion : ${promotion.toMap().toString()}'); // correct topic
await post('https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$firebaseServerKey'
},
body: jsonEncode({
'notification': <String, dynamic>{
'title': sprintf(
AppLocalizations.instance
.text('PROMOTION_PUSH_SUBTITLE'),
[promotion.productName]),
'body': sprintf(
AppLocalizations.instance.text('PROMOTION_PUSH_BODY'),
[promotion.productName, promotion.price, promotion.availableQuantity]),
'sound': 'true'
},
// 'priority': 'high',
'android':{
'priority' : 'high'
},
'apns':{
'headers':{
'apns-priority': '5'
}
},
'webpush': {
'headers': {
'Urgency': 'high'
}
},
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
// parameter to pass with the message
// 'promotionId': promotion.promotionId,
// 'imageUrl': promotion.imageUrl,
// 'isPromotion' : promotion.isPromotion,
// 'productName': promotion.productName,
// 'productCategory': promotion.category,
// 'vendor': promotion.vendor,
// 'price': promotion.price,
// 'description': promotion.productDescription
},
'to': topic // correct
// 'topic': topic // throws error 400
})).whenComplete(() {
print('sendNewPromotion(): message sent');
}).catchError((e) {
print('sendNewPromotion() error: $e');
});
}
}
最佳答案
您在主题消息中所做的一切都是正确的,但您必须将 "to"
键修改为:
"to":"/topics/yourtopicyousubscribed"
关于flutter - 使用 post 请求向 FCM 主题发送的消息未送达 Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63574994/