如何像颤动一样向所有用户推送通知

how to push notification to all users in flutter like

大家好,我想通过 API 像这样向我的 firebase 中的所有用户发送通知:

`var serverToken ="" ; 

sendNotify(String title , String body , String id) async{

await http.post(

Uri.parse('https://fcm.googleapis.com/fcm/send'),

headers:<String,String>{'Content-Type':'application/json',
'Authorization':'key=$serverToken',},

body:jsonEncode(

  <String,dynamic>{

    'notification':<String,dynamic>{

      'body':body.toString(),

      'title':title.toString()

    },

    'priority':'high',

    'data':<String,dynamic>{

      'click_action':'FLUTTER_NOTIFICATION_CLICK',

      'id':id.toString()},

//'to':'all', <<<<<<< 这里我想发送给所有用户而不是通过令牌 }));`

为此你应该使用主题,

您需要为用户订阅all主题,发送通知时不需要令牌

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted in to a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

// subscribe to topic on each app start-up
await FirebaseMessaging.instance.subscribeToTopic('all');

和您的数据


jsonEncode({
     'topic': "all",
    'data': {
      'via': 'FlutterFire Cloud Messaging!!!',
      'count': _messageCount.toString(),
    },
    'notification': {
      'title': 'Hello FlutterFire!',
      'body': 'This notification (#$_messageCount) was created via FCM!',
    },
  });