Firebase 'to' 发送给 Flutter 中的所有人

Firebase 'to' send to everyone in Flutter

目前我有主题可以发送推送通知。特别是

今天我正在将 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' 也适用于特定目标。谢谢!

您可以通过 google 使用此 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;
  }
}

FCM 消息中的 to 属性 决定了它被发送到哪里。 to 属性的取值可以是单个设备的device token,也可以是设备组的ID,也可以是topic。

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