1

I can send an FCM message to my Flutter app when the app has subscribed to "test_fcm_topic" as a topic. But if I subscribe to anything else IE: "redrobin" I don't receive the notification. I've tried sending from both the Flutter app and from Postman. In both cases the terminal shows the instance is received but there is no sound or notification popup.

I'm completely baffled as to why I cannot change the topic to anything other than "test_fcm_topic". Why would it work with one topic but not in the other? How can I even begin to troubleshoot?

Here's the code I use to subscribe;

FCMConfig.init(onBackgroundMessage: firebaseMessagingBackgroundHandler).then((value) {FCMConfig.subscribeToTopic("test_fcm_topic");});

Here's the send code in Flutter;

void send() async {
    await http.post(
      'https://fcm.googleapis.com/fcm/send',
      headers: <String, String>{
        'Content-Type': 'application/json',
        'Authorization': 'key=$serverToken',
      },
      body: jsonEncode(
        <String, dynamic>{
          'notification': <String, dynamic>{
            'body': 'This is a body',
            'title': 'Banana'
          },
          'priority': 'high',
          'data': <String, dynamic>{
             "key_1" : "Value for key_1",
             "key_2" : "Value for key_2"
          },
          'to': '/topics/test_fcm_topic',
        },
      ),
    );
  }

For Postman I use these key pairs in the headers

Key: Authorization Value: key= server key
Key: Content-Type: Value: application/json

And this is the Raw JSON Body;

{
 "to" : "/topics/test_fcm_topic",
 "collapse_key" : "type_a",
 "notification" : {
     "body" : "Body of Your Notification",
     "title": "Banana"
 },
 "data" : {
     "body" : "This is a body",
     "title": "Title of Your Notification in Title",
     "key_1" : "Value for key_1",
     "key_2" : "Value for key_2"
 }
}
Meggy
  • 1,491
  • 3
  • 28
  • 63
  • *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 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 Mar 27 '21 at 15:11
  • I just noticed you actually asked precisely this [question](https://stackoverflow.com/questions/66829715/why-is-it-bad-practice-to-use-an-fcm-server-key-on-a-flutter-android-client) on why that is dangerous a bit earlier, but I will leave the comment as a warning: by including the FCM server key in your app, you're putting your users at risk. – Frank van Puffelen Mar 27 '21 at 15:13

1 Answers1

1

Using your code like this works like a charm when testing on my device:

void send(String serverToken) async {
      Response response = await post(
        Uri.parse('https://fcm.googleapis.com/fcm/send'),
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization':'key=$serverToken'
        },
        body: jsonEncode(
          <String, dynamic>{
            'notification': <String, dynamic>{'body': 'This is a body', 'title': 'Banana'},
            'priority': 'high',
            'data': <String, dynamic>{'audioid': '139', 'title': 'done all over time', 'name': 'Greengirl'},
            'to': '/topics/hi',
          },
        ),
      );
      print(response.body);
    }

But please note, that you must subscribe to the topic 'hi3' used in this example. By running :

FirebaseMessaging.instance.subscribeToTopic('hi3'); on the client you want to receive these broadcasts.

Output of the function above is:

I/flutter (18090): {"message_id":1225534686323630021}

followed by:

D/FLTFireMsgReceiver(18090): broadcast received for message

They even run faster than firebase console push notifications.

Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49