显示后通知自动消失

时间:2018-07-29 20:23:57

标签: android notifications broadcastreceiver android-8.0-oreo

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

}

我已经制作了一个应用程序,该应用程序可以按时显示用户在主要活动中选择的通知,并且只有在用户点击通知时通知才消失。否则,它将正常工作,除非我将手机更新为android Oreo( 8.0)现在通知会在某个时间显示,但是会在一段时间后自动消失。如何使通知不会自动消失(在Android Oreo中),而只有在用户点击通知时才会自动消失?

1 个答案:

答案 0 :(得分:0)

好吧,我知道了..这是电池优化选项,它杀死了我的应用程序,这就是为什么通知消失的原因。.我从自动优化中删除了我的应用程序,现在它可以正常工作。