3

I need to show a notification in status bar even if the app is killed or swiped from tray. I have use onGoing(true) & Flag.onGoing_event. It works nicely in some phones but not in all phones.what to do? I need something like the below picture.

enter image description here

private void showNotificationBarState(String title, String body) {

    int mNotificationId = 5;

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.avatar_profile)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setAutoCancel(true)
                    .setOngoing(true);


    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    //notificationBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;


    notificationBuilder.getNotification().flags |= Notification.FLAG_ONGOING_EVENT;
    notificationBuilder.getNotification().flags |= Notification.FLAG_FOREGROUND_SERVICE;


    assert notificationManager != null;
    notificationManager.notify(mNotificationId /* ID of notification */, notificationBuilder.build());


}
piet.t
  • 11,718
  • 21
  • 43
  • 52
  • are you sending `data payload` in notification? – Mohammed Farhan Mar 07 '18 at 06:39
  • no,just want to show a notification in status bar when i switched on Online state – Taqi Tahmid Tanzil Mar 07 '18 at 06:42
  • then its not possible if app gets killed or is in background. To do that you need to send data payload and use `remoteMessage.getData()` in `onMessageReceived(RemoteMessage remoteMessage)` method. Follow this answer [Here](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – Mohammed Farhan Mar 07 '18 at 06:44

0 Answers0