点击后删除通知

55

我刚开始使用通知功能,现在我正在尝试在通知中心中点击通知后删除通知并启动应用程序。

我尝试使用以下代码:

import android.app.NotificationManager;

public class ExpandNotification {
     private int NOTIFICATION = 546;
     private NotificationManager mNM;

     public void onCreate() {
        mNM.cancel(NOTIFICATION);
        setContentView(R.layout.activity_on);
        //Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
    }

我认为这段代码在点击时会执行另一个类?

PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);

然而通知并没有消失,应用程序也没有启动。但我可以左右滑动它来移除它,但这不是我想要的。

6个回答

122

要使用Notification.BuilderNotificationCompat.Builder实现相同的效果,请在Builder实例上调用setAutoCancel(true)


88

使用标志 Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);

// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

启动应用程序的方法:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);

// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

3
如果您没有指定ContentIntent(只想在点击通知时将其删除),那该怎么办?在这种情况下,auto_cancel标志似乎没有帮助... - Alex Semeniuk
2
回答自己的评论:你不能这样做。实现类似的方式唯一的方法是创建“空”的PendingIntent.setContentIntent(PendingIntent.getActivity(context, id, __new Intent()__, PendingIntent.FLAG_CANCEL_CURRENT));。但是,点击通知也会隐藏通知面板(如果您有很多需要以这种方式清除的通知,则必须将其拉回)。 - Alex Semeniuk
如果您正在使用NotificationCompat.Builder来构建通知,该怎么办? - user3690202

10

这个答案晚了很多,但是我特别写下以下解决方案,因为通知构造函数已经被弃用了,所以使用构建器来实现通知,像下面这样:

 **.setAutoCancel(true)** is used to remove notification on click

整个通知如下:

  private void makeNotification(String title,String msg){

    Intent resultIntent = new Intent(this, MasterActivity.class);

    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setContentIntent(resultPendingIntent)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle(title)
                    .setAutoCancel(true)
                    .setContentText(msg);

    int mNotificationId = 001;
    NotificationManager mNotifyMgr =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());

}

使用标题和消息调用此方法,您将获得完美的通知。


5

您可以直接在代码中添加.setAutoCancel(true)这行来实现通知的点击后自动取消。

必须将此行添加到您的构建器变量中。

例如:

mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
        .setContentText("Hello")
        .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
        .setAutoCancel(true)
        .setContentIntent(pendingIntent);

3

最简单的方法是设置 builder.setAutoCancel(true),这样可以在点击通知后自动取消该通知。我希望这段代码能够帮到你。

通知构建器

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

点击通知打开一个活动

Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);

显示构建器通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());

完整代码以显示具有图标、图像、标题、描述、自动取消并在单击时打开活动的通知:

public void ShowIntentNotification(View v)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(android.R.drawable.btn_star);
        builder.setContentTitle("This is title of notification");
        builder.setContentText("This is a notification Text");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));

        Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(114, builder.build());

    }

-1

对我来说,那是这样的

.setPriority(Notification.PRIORITY_HIGH);

导致通知在点击后无法清除的原因是...请确保使用:

.setPriority(Notification.PRIORITY_DEFAULT);

而且 .setAutoCancel(true) 应该可以工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接