Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

dart - 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. And I also read somewhere that they mentioned about notification_key which is it will enabled to send the notification to the other device. 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. 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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...