Search code examples
flutterpush-notificationfirebase-cloud-messaging

Flutter: FCM send notification to multiple devices not working


I have connected my project to firebase for FCM notification, and build this function to send notification for all users of my app if the admin press a special button:

void sendNotificationToAllUsers() async {
    var url = Uri.parse('https://fcm.googleapis.com/fcm/send');
    const serverKey = 'XXXXXXXXXXXXXXXXXXXX';

    var headers = {
      'Content-Type': 'application/json',
      'Authorization': 'key=$serverKey',
    };

    var body = {
      'notification': {
        'title': 'Hey guys',
        'body': 'How are you?!',
      },
      'to': '/topics/all',
      'click_action' : 'FLUTTER_NOTIFICATION_CLICK'
    };

    try {
      var response = await http.post(
        url,
        headers: headers,
        body: jsonEncode(body),
      );
      if (response.statusCode == 200) {
        print('Notification sent successfully');
      } else {
        print('Failed to send notification: ${response.reasonPhrase}');
      }
    } catch (e) {
      print('Failed to send notification: $e');
    }
  }

and I have added this script to the AndroidManafist.xml:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

but it prints this in the console: Notification sent successfully without sending any notifications!!!

I really searched for that, but didn't catch ideas about sending for many devices in one push from the app not from Firebase console!!!

any help would be appreciated.

Update:

I have been added this line in the very first of the "sendNotificationToAllUsers" function:

await FirebaseMessaging.instance.subscribeToTopic('all');

But it still prints Notification sent successfully without sending the notification !!!


Solution

  • I had solved that by updating that function to this:

    void sendNotificationToAllUsers() async {
        final messaging = FirebaseMessaging.instance;
        await messaging.subscribeToTopic('all');
        await messaging.requestPermission();
    
        var url = Uri.parse('https://fcm.googleapis.com/fcm/send');
        const serverKey = 'XXXXXXXXXXXXXXXXXXXX';
    
        var headers = {
          'Content-Type': 'application/json',
          'Authorization': 'key=$serverKey',
        };
    
        var data = {
          'notification': {
            'title': notifyTitle.text,
            'body': notifyText.text,
          },
          'priority': 'high',
          'to': "/topics/all",
          'click_action': 'FLUTTER_NOTIFICATION_CLICK'
        };
    
        try {
          var response = await http.post(
            url,
            headers: headers,
            body: jsonEncode(data),
          );
          if (response.statusCode == 200) {
            debugPrint('Notification sent successfully');
          } else {
            debugPrint('Failed to send notification: ${response.reasonPhrase}');
          }
        } catch (e) {
          debugPrint('Failed to send notification: $e');
        }
        clearControllers();
        update();
      }