无法删除 Android < 5.0 上的通知

Cannot delete Notification on Android < 5.0

我创建通知。为此,我使用以下 Service:

public class NFCUploadService extends Service implements MeasureDataPostCallback {
    private static final int IMAGE_SEND_NFC_SERVICE_ID = 100;
    private SessionLastMeasureDataPreferences sessionLastMeasureDataPreferences = null;

    @Override
    public void onCreate() {
        super.onCreate();

        this.sessionLastMeasureDataPreferences = new SessionLastMeasureDataPreferences(this);
    }

    @Override
    public void onDestroy() {
        SessionNFCUploadService.getInstance().invalidateInstance();

        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent.getStringExtra(Globals.KEY_EMAIL) != null) {
            SessionNFCUploadService.getInstance().setEmail(intent.getStringExtra(Globals.KEY_EMAIL));
            SessionNFCUploadService.getInstance().setPassword(intent.getStringExtra(Globals.KEY_PASSWORD));
            SessionNFCUploadService.getInstance().setYear(intent.getStringExtra(Globals.KEY_YEAR));
            SessionNFCUploadService.getInstance().setMonth(intent.getStringExtra(Globals.KEY_MONTH));
            SessionNFCUploadService.getInstance().setDay(intent.getStringExtra(Globals.KEY_DAY));
            SessionNFCUploadService.getInstance().setHour(intent.getStringExtra(Globals.KEY_HOUR));
            SessionNFCUploadService.getInstance().setMinute(intent.getStringExtra(Globals.KEY_MINUTE));
            SessionNFCUploadService.getInstance().setValue(intent.getStringExtra(Globals.KEY_VALUE));
            SessionNFCUploadService.getInstance().setMealTime(intent.getStringExtra(Globals.KEY_MEALTIME));
            SessionNFCUploadService.getInstance().setMood(intent.getStringExtra(Globals.KEY_MOOD));

            if(intent.getStringExtra(Globals.KEY_FILE_PATH) != null) {
                SessionNFCUploadService.getInstance().setImageFile(new File(intent.getStringExtra(Globals.KEY_FILE_PATH)));
            } else {
                SessionNFCUploadService.getInstance().setImageFile(null);
            }
        } 

        if (intent.getAction().equals(ForegroundServiceTags.STARTFOREGROUND_ACTION.getValue()) || intent.getAction().equals(ForegroundServiceTags.REPEAT_ACTION.getValue())) {
            Intent notificationIntent = new Intent(this, MeasureDataFragment.class);
            notificationIntent.setAction(ForegroundServiceTags.MAIN_ACTION.getValue());
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

            NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
            style.addLine(this.getResources().getString(R.string.text_email) + ": " + SessionNFCUploadService.getInstance().getEmail());
            style.addLine(this.getResources().getString(R.string.text_date) + ": " + Helper.getDateTimePattern(SessionNFCUploadService.getInstance().getDay(), SessionNFCUploadService.getInstance().getMonth(), SessionNFCUploadService.getInstance().getYear(), SessionNFCUploadService.getInstance().getHour(), SessionNFCUploadService.getInstance().getMinute()));
            style.addLine(this.getResources().getString(R.string.text_measure_value) + ": " + SessionNFCUploadService.getInstance().getValue());

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setContentTitle(this.getResources().getString(R.string.text_measuredata_transfer));
            builder.setTicker(this.getResources().getString(R.string.text_measuredata_transfer_new));
            builder.setWhen(System.currentTimeMillis());
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));
            builder.setContentIntent(pendingIntent);
            builder.setOngoing(true);
            builder.setStyle(style);
            Notification notification = builder.build();

            this.startForeground(IMAGE_SEND_NFC_SERVICE_ID, notification);
            this.startTask(SessionNFCUploadService.getInstance().getEmail(), SessionNFCUploadService.getInstance().getPassword(), SessionNFCUploadService.getInstance().getYear(), SessionNFCUploadService.getInstance().getMonth(), SessionNFCUploadService.getInstance().getDay(), SessionNFCUploadService.getInstance().getHour(), SessionNFCUploadService.getInstance().getMinute(), SessionNFCUploadService.getInstance().getValue(), SessionNFCUploadService.getInstance().getMealTime(), SessionNFCUploadService.getInstance().getMood(), SessionNFCUploadService.getInstance().getImageFile());
        } else if (intent.getAction().equals(ForegroundServiceTags.CANCEL_ACTION.getValue())) {
            this.stopForeground(true);
            this.stopSelf();
        } else if (intent.getAction().equals(ForegroundServiceTags.STOPFOREGROUND_ACTION.getValue())) {
            this.stopForeground(true);
            this.stopSelf();
        }

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onMeasureDataPostCompleted(StatusMeasureDataPost status, MeasureDataPostPOJO measureDataPostPOJO) {
        Intent notificationIntent = new Intent(this, MeasureDataFragment.class);
        notificationIntent.setAction(ForegroundServiceTags.MAIN_ACTION.getValue());
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Intent repeatIntent = new Intent(this, NFCUploadService.class);
        repeatIntent.setAction(ForegroundServiceTags.REPEAT_ACTION.getValue());

        PendingIntent pendingRepeatIntent = PendingIntent.getService(this, 0, repeatIntent, 0);

        Intent cancelIntent = new Intent(this, NFCUploadService.class);
        cancelIntent.setAction(ForegroundServiceTags.CANCEL_ACTION.getValue());

        PendingIntent pendingCancelIntent = PendingIntent.getService(this, 0, cancelIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(this.getResources().getString(R.string.text_measuredata_transfer));
        builder.setTicker(this.getResources().getString(R.string.text_measuredata_transfer_new));
        builder.setWhen(System.currentTimeMillis());
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher));
        builder.setContentIntent(pendingIntent);

        switch(status) {
        case ERROR_CONNECTION_FAILED:
            builder.setContentText(this.getResources().getString(R.string.text_connection_failed));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_DB_CONNECTION_FAILED:
            builder.setContentText(this.getResources().getString(R.string.text_db_connection_failed));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_IO_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_io_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_JSON_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_json_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_PROTOCOL_EXCEPTION:
            builder.setContentText(this.getResources().getString(R.string.text_protocol_exception));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_WRONG_PARAMETERS:
            builder.setContentText(this.getResources().getString(R.string.text_wrong_parameters));
            builder.setOngoing(true);
            builder.addAction(R.drawable.ic_action_repeat, this.getResources().getString(R.string.text_repeat), pendingRepeatIntent);
            builder.addAction(R.drawable.ic_action_cancel, this.getResources().getString(R.string.text_cancel), pendingCancelIntent);
            break;
        case ERROR_MEASUREDATA_ALREADY_EXISTS:
            builder.setContentText(this.getResources().getString(R.string.text_measuredata_already_exists));

            this.stopForeground(false);
            break;
        case MEASUREDATA_TRANSFERED:
            builder.setContentText(this.getResources().getString(R.string.text_data_saved));

            String email = measureDataPostPOJO.getUser().getEmail();

            MeasureDataListEntry measureData = this.sessionLastMeasureDataPreferences.getMeasureData(email);
            MeasureDataListEntry measureDataListEntry = new MeasureDataListEntry();

            measureDataListEntry.setValue(measureDataPostPOJO.getUser().getValue());
            measureDataListEntry.setTime(measureDataPostPOJO.getUser().getTime());

            if(measureData.compareTo(measureDataListEntry) > 0) {
                this.sessionLastMeasureDataPreferences.setMeasureData(email, measureDataListEntry);
            }

            this.stopForeground(false);
            break;
        }

        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(IMAGE_SEND_NFC_SERVICE_ID, notification);
    }

    private void startTask(String email, String password, String year, String month, String day, String hour, String minute, String value, String mealTime, String mood, File imageFile) {
        MeasureDataPostTask task = new MeasureDataPostTask(email, password, year, month, day, hour, minute, value, mealTime, mood, imageFile);
        task.setCallback(this);
        task.execute(this);
    }
}

到目前为止,我只是用 android 5.0 版及更高版本测试了设备,一切正常。当通知弹出时,我只需将其滑开即可将其删除。

但是...我无法在装有 Android 4.4.2 的 Galaxy Ace 设备上删除这些通知。它也不适用于 Android 4.4.4 的 Samsung Active Tab。没有删除通知的按钮,也没有滑动删除功能。要删除通知,我需要终止应用程序。

有人知道为什么它不能在 android 版本 < 5.0 上运行吗?我该如何解决它?

去掉setOngoing(true)。通过该调用,您是在说您不希望 Notification 能够被清除。