0

I set up an awesome notification package for Push Notifications, I'm using Firebase Messaging services, and everything works fine for iOS, but for Android, I have a problem, when I use the campaign test to send notifications 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?

PedroCova
  • 51
  • 1
  • 1
  • 9
  • *firebaser here* Calls to the FCM REST API require that you specify the FCM *server** key in your code. As its name implies, this key should only be used in server-side code, or in an otherwise trusted environment. The reason for this is that anyone who has the FCM server key can send whatever message they want to all of your users. By including this key in your Android app, a malicious user can find it and **you're putting your users at risk**. See https://stackoverflow.com/a/37993724 for a better solution. – Frank van Puffelen Oct 09 '22 at 14:05
  • yes, for that what I did I use the package flutter_dotenv, so I put the FCM server key in a .env file and I call it using String messageKey = dotenv.env['MESSAGE_KEY'] ?? ''; – PedroCova Oct 09 '22 at 18:42
  • Any time you use that key in a client-side application, it has the same security risk. You can't secure send messages from device to device. The correct architecture is explained in the link I gve above. – Frank van Puffelen Oct 09 '22 at 20:09
  • thanks for the heads up, I check that using dotenv is a really bad idea as anyone can have access to the assets where the keys are stored, and I have other API keys saved there, so I check for other way to properly save the API keys and I found this article, if you have time please let me know what you think about it, I'm planning to use the option 3 method https://codewithandrea.com/articles/flutter-api-keys-dart-define-env-files/ – PedroCova Oct 09 '22 at 20:51

1 Answers1

1

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

pmatatias
  • 3,491
  • 3
  • 10
  • 30
  • Thanks for the info, I was able to fix it, but I had to do some additional steps like directing the app icon to the current app icon, but now it works – PedroCova Oct 11 '22 at 14:11