Notification not showing when the app is closed
Supporting Fire-base push notification for all device is like a maze.
There are 3 scenario for push notification
- Foreground (fire
FirebaseMessagingService
'sonMessageReceived
method) - Background (can't fire
FirebaseMessagingService
but firebase service push their message into device's Notification Tray) - App kill (can't fire
FirebaseMessagingService
firebase service push push their message into device's Notification Tray)
NOTE: Some chinese device like Xiaomi, Asus, Huawei etc. are force stop your app when you swipe out from your background stack, so that the service is kill. For that reason firebase service is not run and can't push their notification into Notification tray.
In Xiaomi - security-->permissions-->autostart then check mark your app. Enable this means app's any service always run in background. Then you get all kinds of notification from your app every time.
If you want to learn more about that, Check that
and if you want to go particular device's security page from your app then check this.
Notification in Samsung and Xiaomi show This app hasn't send you any notification
The code won't work on Android 8 (API 26) and above without first registering a notification channel. On Android before that, it will work just fine.
Some of the code snippets below are taken from here.
Creating a notification channel
Register notification channels inside your activity before attempting to show any notification. Alternatively, you can register them inside a custom Application
class before any activities or services start (but this won't be shown here for the sake of simplicity).
@Override
public void onCreate(Bundle savedInstanceState) {
createNotificationChannel(); // Registering a notification channel
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Deliver the notification
public void showNotification() {
// ...
// NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
// Replace with line below.
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID);
// ...
}
Related Topics
Clarify the Firebase Connections
How to Change the Background of an Android Tab Widget
Android Viewpager Get the Current View
Map View Draw Directions Using Google Directions API - Decoding Polylines
Android Xml VS Java Layouts Performance
How to Disable Home Button in Android
Convert Time Value to Format "Hh:Mm Am/Pm" Using Android
Execution Failed for Task App:Transformclasseswithdesugarfordebug Error on Android Studio 3.0 Beta 2
How to Change the Text of a Checkbox in Listview
Android Device Does Not Show Up in Adb List
Put a Progressbar on Actionbar
How to Change Separator Color in Navigationview
Textinputlayout :How to Give Padding or Margin to Hint
How to Pop Fragment Off Backstack
Android Linkify Text - Spannable Text in Single Text View - as Like Twitter Tweet
Message "Error: Resource Android:Attr/Lstar Not Found"