单击它后关闭通知

时间:2018-02-02 08:01:01

标签: java android android-intent android-notifications

我创建了一个通知,但是一旦点击它就不会删除!我甚至试过notification.setAutoCancel(true)notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL

这是我的代码:

notification.setSmallIcon(R.drawable.message64);
Uri uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.setSound(uri);

Resources resources = getApplicationContext().getResources(),
            systemResources = Resources.getSystem();
notification.setTicker("Notification");
notification.setContentTitle("Nouveau message");
notification.setContentText("Vous avez reçu un nouveau message. ");
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification.getNotification().flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(uniqueID,notification.build());

它运行良好但我点击它时仍然无法删除它。

1 个答案:

答案 0 :(得分:0)

 NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setWhen(System.currentTimeMillis())
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.mipmap.ic_launcher))
            .setContentTitle(getString(R.string.app_name))
            .setAutoCancel(true)

            .setContentText("Test")



    getManager().notify(ID, builder.build());

使用setAutoCancel(true)会在点击

时清除通知