How can I prevent to display FCM notification on Flutter app background?

1,057

from the sender device u have to send data only notification

await http.post(
          Uri.parse('https://fcm.googleapis.com/fcm/send'),
          headers: <String, String>{
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'key=$serverKey',
          },
          body: jsonEncode(
            <String, dynamic>{
              'data': <String, dynamic>{
                'id': '2',
                'status': 'done'
              },
              'to': userToken,
              "collapse_key": uid,
            },
          ),
        );
Share:
1,057
saeid
Author by

saeid

Updated on December 30, 2022

Comments

  • saeid
    saeid over 1 year
    Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      print('Handling a background message ${message.messageId}');
    }
    
    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
    

    I can display background notifications with these snippet codes. But is there any way to not display some specific notification in the background?

    For example I don't want to display notifications with data = {title = "call"}

    • Ali Izadyar
      Ali Izadyar almost 2 years
      found the solution? I have the same problem
  • saeid
    saeid almost 3 years
    in _firebaseMessagingBackgroundHandler function we don't implement anything to display notification, automatically displayed if we pass the empty function to FirebaseMessaging.onBackgroundMessage
  • Benyamin Beyzaie
    Benyamin Beyzaie over 2 years
    this method is not related to this topic if you don't write this function at all the notification will appear! so it is not helpful...
  • PeakGen
    PeakGen almost 2 years
    Data only notifications will not show up in iOS if the device is rebooted. It will show again if the user opens the app.
  • Husen
    Husen almost 2 years
    @PeakGen The purpose of data-only notification is not to show up, I tested this approach and it worked on android, I am not sure about IOS.