Search code examples
androidnotificationsbroadcastreceiverandroid-8.0-oreo

Notification auto disappear after showing


public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        intent = new Intent(context, AlertDialog.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
        intent.putExtra("STARTED_BY_RECEIVER", true);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        String CHANNEL_ID = "channel id";// The id of the channel.
        CharSequence name = "Channel";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);

        Notification notification = new Notification.Builder(context)
                .setContentIntent(pendingIntent)
                .setContentText("Some Text")
                .setContentTitle("Notification")
                .setAutoCancel(true)
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_notification_praying)
                .setChannelId(CHANNEL_ID)
                .build();
        notificationManager.createNotificationChannel(mChannel);

        notificationManager.notify(100 , notification);


    } else {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);

        intent = new Intent(context, AlertDialog.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // added new line
        intent.putExtra("STARTED_BY_RECEIVER", true);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_notification_praying)
                .setContentIntent(pendingIntent)
                .setContentText("Some Text")
                .setContentTitle("Notification")
                .setAutoCancel(true)
                .setOngoing(true);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);
        notificationManager.notify(100, builder.build());
    }

}

I've made an application which show notification on time that user chose in main activity and Notification should disappear only when user tap on notification otherwise not..and it was working fine unless I've updated my phone to android Oreo (8.0) now notification show at time but auto disappear after sometime.. How to make notification not auto disappear (in android Oreo) but only when user tap on it?


Solution

  • alright I figure this out.. it was battery optimize option which was killing my application so that's why notification was disappearing.. I removed my application from auto optimize now it's working fine.