16

I am trying to implement firebase push notifications for a flutter app.

But it appears just as an icon in the status bar. How can i make it pop up?

enter image description here

I want popup on the screen when notification is received.

Here is how my code looks like:

 Future<bool> sendNotification(var userToken, String bodyMessage) async {
 final data = {
  "notification": {
    "body": bodyMessage,
    "title": "Leave Application",
  },
  "priority": "high",
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "id": "1",
    "status": "done"
  },
  "registration_ids": userToken,
 };

 final headers = {
  'content-type': 'application/json',
  'Authorization':
  'key=<Firebase web key>',
 };

 final response = await http.post(postUrl,
    body: json.encode(data),
    encoding: Encoding.getByName('utf-8'),
    headers: headers);

 if (response.statusCode == 200) {
  print(response.statusCode.toString());
  print("Working");
  return true;
 } else {
  print(postUrl.toString());
  print(response.statusCode.toString());
  print("Not Working");
  return false;
 }
}
Hetal
  • 471
  • 4
  • 14

4 Answers4

14

All notifications will be sent to Miscellaneous channel if android_channel_id is not specified in the FCM HTTP API. Miscellaneous channel comes with a default level of importance which only has sound but will not pop on screen.

To make the notification pop on screen, you need to create a notification channel with importance of max/high. For detailed steps on how to create a notification channel, you may refer to the Notification Channel Documentation by FlutterFire.

After the notification channel is created, you can add the channel id (eg: high_importance_channel) into the FCM HTTP API request body and the notifications that sent to this channel should start to pop on screen when your app is in the background.

{
  "notification": {
    "android_channel_id": "high_importance_channel",
    "title": "Notification Title",
    "body": "Notification body ...",
    "sound": "default",
    "priority": "high",
    "time_to_live": 86400,
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
  },
  "data": {
    "post_id": 10000012,
    "type": "NEWS",
  },
  "condition": "'dogs' in topics"
}

1st image 2nd image

luke77
  • 2,255
  • 2
  • 18
  • 30
  • This works perfectly . I am working on C# server side and Xamarin Mobile. THe main point here is we need to create Miscellaneous channel with Max prioirty so that if server side sent the message it will be delivered to Miscellaneous channel which changed to Max thats why it will pop up. – Naveed Khan Sep 12 '22 at 13:55
1

I tried to reproduce the problem and dug deeper and from what I found you can't achieve what you want in Flutter. Although, you can handle Firebase onMessageReceived in Java/Kotlin and then show the notification that you want.

Please refer here: FCM for android: popup system notification when app is in background

Albert221
  • 5,223
  • 5
  • 25
  • 41
  • I added the sound in my data payload and set to default, but it still does not head up. – Djiveradjane Canessane Mar 26 '20 at 15:09
  • this is my payload: DATA='{"notification": {"body": "test chat","title": "test chat"}, "priority": "high", "data": {"click_action": "FLUTTER_NOTIFICATION_CLICK", "page": "chat", "sound": "default", "numB": "numB", "numA": "numA", "prenomB": "Djiva"}, "to": "userToken"}' curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X POST -d "$DATA" -H "Authorization: key= yourKey" – Djiveradjane Canessane Mar 26 '20 at 15:10
  • Later I'll try to fully reproduce your problem and find an answer. – Albert221 Apr 03 '20 at 13:44
  • @JeevaCanessane Okay, I tried to reproduce the problem and dug deeper and from what I found you can't achieve what you want in Flutter. Although, you can handle Firebase `onMessageReceived` in Java/Kotlin and then show the notification that you want. Please refer here: https://stackoverflow.com/a/42323617/3158312 – Albert221 Apr 03 '20 at 18:50
1

Create a notification channel with importance of max/high.

AndroidNotificationChannel channel = AndroidNotificationChannel(
    Random.secure().nextInt(100000).toString(),
    "High Notification Channel",
   importance: Importance.max,
);
AndroidNotificationDetails androidNotificationDetails = AndroidNotificationDetails(
    channel.id.toString(),
    channel.name.toString(),
    channelDescription: "Notes App Channel",
    importance: Importance.high,
    priority: Priority.high,
    icon: '@mipmap/ic_launcher',
    ticker: 'ticker',
);
user16217248
  • 3,119
  • 19
  • 19
  • 37
ahmar jani
  • 11
  • 1
-1

You can check weather app is in battery optimization mode or not. If yes remove it from there.

Steps are as follows: Settings -> Battery -> Battery Optimization -> "Select your App" -> Click on "Don's Optimise"

Thanks.