简体   繁体   中英

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 :)

    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 :

    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 :

 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 :

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

You can clean particular notification based on its NOTIFICATION_ID

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

To clean all notification use cancelAll()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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