I use firebase_messaging
for push notifications and to send the messages I use POST
requests.
When sending messages to FCM token
it all works properly, but when sending them to topic
(I basically modified the request I use for token
messages) they won't get delivered.
I checked and the topic is correct and on physical Android device I'm correctly subscripted to that topic, as when sending a topic message from FCM console it gets delivered immediately. Can you spot what I'm doing wrong here?
Many thanks.
token message (working):
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');
});
}
topic message (not delivered) :
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');
});
}
}
Solution 1: Krish Bhanushali
everything you have done is correct in the topic messaging, but you have to modify your "to"
key to this :
"to":"/topics/yourtopicyousubscribed"