Hetal
Hetal

Reputation: 511

Firebase push notification is not popup on the screen [Flutter]

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;
 }
}

Upvotes: 19

Views: 12178

Answers (4)

ahmar jani
ahmar jani

Reputation: 11

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',
);

Upvotes: 1

luke77
luke77

Reputation: 3703

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

Upvotes: 16

Albert221
Albert221

Reputation: 7167

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

Upvotes: 1

Deepak Shakyavanshi
Deepak Shakyavanshi

Reputation: 42

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.

Upvotes: -1

Related Questions