Xiaomi Does Not Receive Notification When Application Is Not Running

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

  1. Foreground (fire FirebaseMessagingService's onMessageReceived method)
  2. Background (can't fire FirebaseMessagingService but firebase service push their message into device's Notification Tray)
  3. 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



Leave a reply



Submit