简体   繁体   English

使用 FCM 和 Flutter 通过设备发送通知

[英]Send notification over device using FCM and Flutter

currently I am trying to send a notification to multiple device.目前我正在尝试向多个设备发送通知。 I already read the documentation on how to send notification to device group , they mentioned that I need to use registration_ids: [...] instead of to: token .我已经阅读了有关如何向设备组发送通知的文档,他们提到我需要使用registration_ids: [...]而不是to: token And I also read somewhere that they mentioned about notification_key which is it will enabled to send the notification to the other device.而且我还在某处读到了他们提到的关于notification_key的内容,它可以将通知发送到其他设备。 So, I'm stuck on finding the key.所以,我一直在寻找钥匙。 But then, after few days browsing, I found out that here stated that the notification_key already deprecated.但是,经过几天的浏览,我发现here声明notification_key已被弃用。 So, I would like to ask if any of you guys know how to send notification to multiple device without using console.所以,我想问一下你们中是否有人知道如何在不使用控制台的情况下向多个设备发送通知。

This I my code segment to send push the notification:这是我发送推送通知的代码段:

try {
  await http.post(
    Uri.parse('https://fcm.googleapis.com/fcm/send'),
    // Uri.parse('https://fcm.googleapis.com/fcm/notification'),

    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': 'key=$serverKey',
      'project_id':'....',
    },
    body: jsonEncode(
      <String, dynamic>{
        'notification': <String, dynamic>{
          'body': 'this is a body2',
          'title': 'this is a title'
        },
        'priority': 'high',
        'data': <String, dynamic>{
          'click_action':
          'FLUTTER_NOTIFICATION_CLICK',
          'id': '1',
          'status': 'done'
        },
        'registration_ids': ['fbN9aYoORhihksrTo7j5D2:APA91b......', 'fArqpgKrTJ0fL8SUKddy2F:APA91bFWf1cnVMS8.......'],
        // 'to': _token,
      },
    ),
  );
} catch (e) {
  print("error push notification");
}

It work fine if I used to instead of registration_ids , but the things is as I understand is to is used if I want to send notification only for one device.如果我过去代替registration_ids ,它工作正常to但据我所知,如果我只想为一台设备发送通知,则使用to。

I already stuck with this issue for three days and still not found any solution.我已经在这个问题上坚持了三天,仍然没有找到任何解决方案。 Most of them are using console instead.他们中的大多数人都在使用控制台。 Your help will really made my day.你的帮助真的会让我很开心。 Thank you in advance!先感谢您!

I found a solution and its work!我找到了解决方案及其工作!

 var serverKey =
    'AAAAUb...';
QuerySnapshot ref =
    await FirebaseFirestore.instance.collection('users').get();


try {
  ref.docs.forEach((snapshot) async {
    http.Response response = await http.post(
      Uri.parse('https://fcm.googleapis.com/fcm/send'),
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$serverKey',
      },
      body: jsonEncode(
        <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': snapshot.data() ['tokenID'],
        },
      ),
    );
  });
} catch (e) {
  print("error push notification");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 FCM 和 Flutter(一个到另一个设备)发送推送通知? - How to send push Notification using FCM and Flutter(One to Another Device)? 如何使用Flutter和Firebase实时数据库发送FCM通知 - How to send fcm notification using flutter and firebase realtime database 如何在flutter中使用javascript云函数发送fcm通知? - How to send a fcm notification using javascript cloud functions in flutter? 如何使用 FCM 发送通知? - How to send notification using FCM? 使用不带 FCM 的 flutter 发送推送通知 - Send push notification with flutter without FCM 使用Flutter和FCM终止应用时,Android设备未收到通知消息 - Android device not receiving notification message when the app is terminated using Flutter and FCM 使用 FCM 按主题进行 Flutter 推送通知 - Flutter push notification using FCM by topic Flutter - 在 IOS 上使用 FCM 进行后台通知 - Flutter - background notification using FCM on IOS 如何使用 FCM 在颤振中推送通知 - How to push notification in flutter using FCM 为什么我需要将管理 SDK 与 Cloud Functions 结合使用才能在 Flutter 中使用 FCM 向其他设备发送通知? - Why do I need to use admin SDK with Cloud Functions to send notifications to other device using FCM in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM