[英]How to clean an Android notification?
提前致謝。 我對此並不陌生,所以,我要直截了當,我正在嘗試創建一個醫療應用,因此,當此人在通知中點擊“否”時,這將轉到該應用以編輯其提醒(工作),但是當我點擊“是”時,將清除通知,那么,我該怎么做? 謝謝
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);
}
您應該將通知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);
}
您可以根據NOTIFICATION_ID
清除特定通知
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_ID);
要清除所有通知,請使用cancelAll()
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.