为什么我的应用程序的通知会自动清除?

时间:2019-01-25 11:30:34

标签: android android-notifications

根据android文档,

  

在应用程序或用户将其取消之前,通知在通知抽屉中仍然可见。

但是为什么我的通知会被自己清除?我的代码是这样的

public class Worker extends Service{

    private final int notification_id=((int)(Calendar.getInstance().getTimeInMillis())%Integer.MAX_VALUE);
    private final String channel_id="STORAGE_ORGANIZER_DUPLICATE_CONTENT_EXTRACTOR_SERVICE_NOTIFICATION";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        final NotificationCompat.Builder notification_builder=
            new NotificationCompat.Builder(this, channel_id)
                    .setSmallIcon(R.drawable.storage_icon)
                    .setContentTitle(getString(R.string.duplicate_content_extractor_notification_msg1))
                    .setProgress(1,0,true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setOngoing(true);

    startForeground(notification_id,notification_builder.build());

    new Thread(new Runnable() {
                @Override
                public void run() {
                    // some long running work
                    Intent remover_intent=new Intent(
                        DuplicateContentExtractorService.this,
                        DuplicateContentExtractorServiceStopperService.class
                    );
                    PendingIntent remover_pendingIntent=PendingIntent.getService(
                        DuplicateContentExtractorService.this,
                        (int)Calendar.getInstance().getTimeInMillis() % 9999,
                        remover_intent,
                        PendingIntent.FLAG_ONE_SHOT
                    );
                    stopForeground(false);
                    notification_builder.setOngoing(false);
                    notification_builder.setDeleteIntent(remover_pendingIntent);
                    notificationManager.notify(notification_id,notification_builder.build());

                }
    }).start();
}

1 个答案:

答案 0 :(得分:0)

您需要将notification_builder.setOngoing(true);设置为true,例如

public class Worker extends Service{

    private final int notification_id=((int)(Calendar.getInstance().getTimeInMillis())%Integer.MAX_VALUE);
    private final String channel_id="STORAGE_ORGANIZER_DUPLICATE_CONTENT_EXTRACTOR_SERVICE_NOTIFICATION";

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        final NotificationCompat.Builder notification_builder=
            new NotificationCompat.Builder(this, channel_id)
                    .setSmallIcon(R.drawable.storage_icon)
                    .setContentTitle(getString(R.string.duplicate_content_extractor_notification_msg1))
                    .setProgress(1,0,true)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setOngoing(true);

    startForeground(notification_id,notification_builder.build());

    new Thread(new Runnable() {
                @Override
                public void run() {
                    // some long running work
                    Intent remover_intent=new Intent(
                        DuplicateContentExtractorService.this,
                        DuplicateContentExtractorServiceStopperService.class
                    );
                    PendingIntent remover_pendingIntent=PendingIntent.getService(
                        DuplicateContentExtractorService.this,
                        (int)Calendar.getInstance().getTimeInMillis() % 9999,
                        remover_intent,
                        PendingIntent.FLAG_ONE_SHOT
                    );
                    stopForeground(false);
                    notification_builder.setOngoing(true);
                    notification_builder.setDeleteIntent(remover_pendingIntent);
                    notificationManager.notify(notification_id,notification_builder.build());

                }
    }).start();
}