-->

Android remove notification(created by mNotifyBuil

2020-08-22 06:27发布

问题:

i found many answer to this, but none help :( I have this code:

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.icon;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int notifyID = 96;
    NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
        .setContentTitle("MyApp")
        .setContentText(message)
        .setDefaults(Notification.DEFAULT_ALL)
        .setAutoCancel(true)
        .setSmallIcon(icon);

    Notification notification = mNotifyBuilder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    mNotificationManager.notify(notifyID, notification);
}

But if i click on notification nothing happen and it is still there. In documentation is, that i need to use:

.setAutoCancel(true)

Some one have similar problem and somebody tells him to use:

notification.flags |= Notification.FLAG_AUTO_CANCEL;

I use both, but no result :( Thank you very much for answers. :)

回答1:

I think that if you don't use an Intent with your notification, the only way to dismiss its to swipe it.

Otherwise you can use an Intent to open your activity and that will actually clear the notification:

Intent showIntent = new Intent(this, YourActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);

And to add it to the notification:

NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
    .setContentTitle("MyApp")
    .setContentText(message)
    .setDefaults(Notification.DEFAULT_ALL)
    .setAutoCancel(true)
    .setContentIntent(contentIntent)
    .setSmallIcon(icon);

Hope this helps!



回答2:

The user can dismiss all notification or if you set your notification to auto-cancel it is also removed once the user selects it.

You can also call the cancel() for a specific notification ID on the NotificationManager. The cancelAll() method call removes all of the notifications you previously issued.

Example:

mNotificationManager.cancel(notifyId);


回答3:

I generally use

mNotificationManager.cancleAll();


回答4:

just use this line inside button onclick event-

mNotificationManager.cancel(notifyId);//id is that u used for notification


回答5:

just add

 Intent showIntent = new Intent();
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, showIntent, 0);


    myNotification = new Notification.Builder(getApplicationContext())
                    .setContentTitle("Title")
                    .setContentText("Text")
                    .setTicker("notificatio")
                    .setContentIntent(contentIntent)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .build();

add empty pending intent then on click on notification, notification will remove. it works for me.