Send Push Notifications Flutter Android

Issue

I set up awesome notification package for Push Notifications, I’m using Firebase Messaging services, everything works fine for iOS, but for Android I have a problem, when I use the campaign test to send notification to an Android device, it works fine, I received the notification when the app is in the background, the problem is when I send a notification from the app, the Android device doesn’t get it, here is the code that I’m using to send the notification:

  Future<void> sendAndroidNotification(
      authorizedSupplierTokenId, serviceName) async {
    try {
      http.Response response = await http.post(
        Uri.parse(messageAndroidUrl),
        headers: <String, String>{
          'Content-Type': 'application/json; charset=UTF-8',
          'Authorization': 'key=$messageKey',
        },
        body: jsonEncode(
          <String, dynamic>{
            'notification': <String, dynamic>{
              'body': serviceName,
              'title': 'Nueva Solicitud',
            },
            'priority': 'high',
            'data': <String, dynamic>{
              'click_action': 'FLUTTER_NOTIFICATION_CLICK',
              'id': '1',
              'status': 'done'
            },
            'to': authorizedSupplierTokenId,
            'token': authorizedSupplierTokenId
          },
        ),
      );
      response;
    } catch (e) {
      e;
    }
  }

The var messageAndroidUrl = "https://api.rnfirebase.io/messaging/send"; I get this Url from the firebase_messaging pub package example, but I had also tried this URL "https://fcm.googleapis.com/fcm/send"; which is the one that works for iOS, so can someone help me on this?

Solution

yup, you may missed 1 more step.

documentation said: android configuration

If the application is currently in the foreground, a visible
notification is not presented.

Although this is outside of the scope of FlutterFire, we can take
advantage of the flutter_local_notifications package to help us:

you may see full step here : https://firebase.flutter.dev/docs/messaging/notifications#android-configuration

Answered By – pmatatias

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *