0

The question is same as here Call onMessage method when the app is in background in flutter and I am following the thread but since there is no response, I am asking this question again.

I've connected my Flutter app with Firebase Messaging and I am receiving notification when my app is in Foreground using the Flutter Local Notifications Plugin. But I cannot receive any notification when my app is in Background

Following is my code:

// FIREBASE CONFIGURATION & HANDLING
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    debugPrint("onMessage: $message");
    final notification = message['data']['notification'];

    Map responseBody = convert.jsonDecode(notification);

    _showNotification(
      icon: responseBody['icon'],
      title: responseBody['title'],
      body: responseBody['body'],
    );

    setState(() {});
  },
);


// DISPLAY NOTIFICATION - HEADS UP
Future<void> _showNotification({String icon, String title, String body}) async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your channel id',
    'your channel name',
    'your channel description',
    importance: Importance.Max,
    priority: Priority.High,
    ticker: 'ticker',
  );

  var iOSPlatformChannelSpecifics = IOSNotificationDetails();

  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);

  await flutterLocalNotificationsPlugin.show(
    0,
    title,
    body,
    platformChannelSpecifics,
  );
}

Can someone please guide me how can I receive notifications when my app is in Background

Zain SMJ
  • 1,492
  • 7
  • 19
  • 33

1 Answers1

0

Resolved!!

The issue was with the format of curl calling. As per the instructions given here

Changed this:

{
  "data": {
    "notification": {
        "title": "Notification Title",
        "body": "Notification Body",
    },
        "click_action": "FLUTTER_NOTIFICATION_CLICK"
  },
  "to": "YOUR_DEVICE_TOKEN"
}

To this:

{
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "id": "1",
    "status": "done",
  },
  "notification": {
    "body": "Notification Body",
    "title": "Notification Title"
  },
  "priority": "high",
  "to": "YOUR_DEVICE_TOKEN"
}
Zain SMJ
  • 1,492
  • 7
  • 19
  • 33