如何清除Android通知?

时间:2018-07-14 05:55:43

标签: javascript android android-layout

先谢谢了。 我对此并不陌生,所以,我要直截了当,我正在尝试创建一个医疗应用,因此,当此人在通知中点击“否”时,这将转到该应用以编辑其提醒(工作),但是当我点击“是”时,将清除通知,那么,我该怎么做?谢谢

PS:我是西班牙用户,对我的英语不好表示抱歉:)

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Uri uri = intent.getData();

    //Muestra una notificación para ver los detalles en la barra de tareas
    Intent action = new Intent(this, MainActivity.class);
    action.setData(uri);
    PendingIntent operation = TaskStackBuilder.create(this)
            .addNextIntentWithParentStack(action)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    //Grabe la descripción de la tarea
    if(uri != null){
        cursor = getContentResolver().query(uri, null, null, null, null);
    }

    String description = "";
    try {
        if (cursor != null && cursor.moveToFirst()) {
            description = ContenidoRecordatorio.getColumnString(cursor, ContenidoRecordatorio.AlarmReminderEntry.KEY_TITLE);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    Intent secondActivityIntent = new Intent(this, MainActivity.class);
    PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);

    Intent thirdActivityIntent = new Intent(this, MainActivity.class);
    PendingIntent thirdActivityPendingIntent = PendingIntent.getActivity(this, 0 , thirdActivityIntent, PendingIntent.FLAG_ONE_SHOT);

    // Muestra la notificacion - ver strings.xml
    Notification note = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.reminder_medicamento))
            .setContentText(description)
            .addAction(R.drawable.ic_add_alert_black_24dp,"Yes",secondActivityPendingIntent)
            .addAction(R.drawable.ic_add_alert_black_24dp,"No",thirdActivityPendingIntent)
            .setSmallIcon(R.drawable.ic_add_alert_black_24dp)
            .setContentIntent(operation)
            .setAutoCancel(true)
            .build();

    manager.notify(NOTIFICATION_ID, note);
}

2 个答案:

答案 0 :(得分:0)

您应该将通知ID存储在“是”意图的捆绑包中,如下所示:

    Intent secondActivityIntent = new Intent(this, MainActivity.class);
    secondActivityIntent.putExtra("NotificationId",Notification_ID)
    PendingIntent secondActivityPendingIntent = PendingIntent.getActivity(this, 0 , secondActivityIntent, PendingIntent.FLAG_ONE_SHOT);

然后在您的MainActivity中,应按以下方式获取通知的ID:

 int notificationId=getIntent().getIntExtra("NotificaitonId",-1);

然后,如果notificationId不等于-1,则表示此活动是在通知中进行的,因此您可以使用此ID调用notificationManger.cancel(),这样将从状态栏中删除通知:

  if(notificationId!=-1){
            NotificationManager notificationManager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(notificationId);
        }

答案 1 :(得分:0)

您可以根据特定通知的NOTIFICATION_ID

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);

要清除所有通知,请使用cancelAll()

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();