所以我创建了一个应用程序,用户可以在其中创建通知作为提醒。该通知会弹出并运行,但我想在用户单击某个操作时删除该通知。这是我的代码:
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification notif = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification))
.setOngoing(switchState)
.setContentTitle("Noteify")
.setContentText(notifEditText.getText().toString())
.setPriority(Notification.PRIORITY_MAX)
.addAction(R.drawable.delete,"Delete",pIntent)
.setContentIntent(pIntent).getNotification();
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,notif);
我添加了操作,但我不知道如何删除通知。
答案 0 :(得分:0)
简单,只需称呼:
mBuilder.setAutoCancel(true);
在此
下面NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
.setContentTitle("MyApp")
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(icon);
答案 1 :(得分:0)
Gowthaman M说的是正确的,将setAutoCancel(true);
添加到Notification
对象。
只需编辑您的代码,如下所示
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
Notification notif = new Notification.Builder(MainActivity.this)
.setSmallIcon(R.drawable.notification)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.notification))
.setOngoing(switchState)
.setContentTitle("Noteify")
.setAutoCancel(true)
.setContentText(notifEditText.getText().toString())
.setPriority(Notification.PRIORITY_MAX)
.addAction(R.drawable.delete, "Delete", pIntent)
.setContentIntent(pIntent).getNotification();
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(0,notif);