简体   繁体   English

如何清除Android通知?

[英]How to clean an Android notification?

Thanks in advance. 提前致谢。 I'm new on this, so, let me go to the point, i'm trying to create an medical app, so, when the person tap "No" in the notification, this go to the app to edit their reminder (that's work), but when i tap "Yes", cleans the notification, so, how i can do that? 我对此并不陌生,所以,我要直截了当,我正在尝试创建一个医疗应用,因此,当此人在通知中点击“否”时,这将转到该应用以编辑其提醒(工作),但是当我点击“是”时,将清除通知,那么,我该怎么做? Thanks 谢谢

PS: I'm a spanish user, srry for my bad english :) 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);
}

you should store your notification id in bundle of the Yes intent like this : 您应该将通知ID存储在“是”意图的捆绑包中,如下所示:

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

then in your MainActivity you should get the Id of your notificaiton like this : 然后在您的MainActivity中,您应该像这样获得通知的ID:

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

then if notificationId was not equal to -1 it means this activity was lunched from notification so you can call the notificationManger.cancel() with this id and it will remove your notification from status bar like this : 然后,如果notificationId不等于-1,则表示此活动是在通知中进行的,因此您可以使用此ID调用notificationManger.cancel(),这样将从状态栏中删除通知:

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

You can clean particular notification based on its NOTIFICATION_ID 您可以根据NOTIFICATION_ID清除特定通知

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

To clean all notification use cancelAll() 要清除所有通知,请使用cancelAll()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM