Firebase 'to'发送给 Flutter 中的每个人



当前我有发送推送通知的主题。特别是

  • all:这是我向每个人发送通知的主题
  • paid:这是一个我只向一小群用户发送通知的主题

今天我正在将Flutter升级到最新的firebase包,我遇到了这个:

<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body',
'title': 'this is a title'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': await firebaseMessaging.getToken(),
},

等等,'to'不是只向特定设备发送通知吗?为什么

'to': await firebaseMessaging.getToken(),

这会向所有设备发送消息吗?我很困惑,因为医生说"to"也是针对特定目标的。谢谢

FCM消息中的to属性决定了消息的发送位置。to属性的值可以是单个设备的设备令牌,也可以是设备组的ID,也可以为主题。

听起来,在实际代码中,您在to中传递了一个主题,而在问题中的示例中,它传递了设备令牌。

您可以使用谷歌的Firestore api发送推送通知

Future<bool> callOnFcmApiSendPushNotifications(List <String> userToken) async 
{
final postUrl = 'https://fcm.googleapis.com/fcm/send';
final data = {
"registration_ids" : userToken,
"collapse_key" : "type_a",
"notification" : {
"title": 'NewTextTitle',
"body" : 'NewTextBody',
}
};
final headers = {
'content-type': 'application/json',
'Authorization': constant.firebaseTokenAPIFCM // 'key=YOUR_SERVER_KEY'
};
final response = await http.post(postUrl,
body: json.encode(data),
encoding: Encoding.getByName('utf-8'),
headers: headers);
if (response.statusCode == 200) {
// on success do sth
print('test ok push CFM');
return true;
} else {
print(' CFM error');
// on failure do sth
return false;
}
}

最新更新