52

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

我尝试使用以下代码:

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);

但是,通知不会消失,应用程序也不会启动。但是我可以向左或向右滑动以将其删除,但这不是我想要的..

4

6 回答 6

117

Notification.Builder使用或NotificationCompat.Builder调用setAutoCancel(true)Builder 实例获得相同的效果。

于 2014-07-14T09:46:09.567 回答
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);
于 2012-10-19T11:07:51.210 回答
10

这个答案太晚了,但我特别编写了以下解决方案,因为通知构造函数已被弃用,因此使用 builder 使用通知,如下所示:

 **.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());

}

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

于 2015-11-27T11:00:50.060 回答
2

最好和简单的方法是设置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());

    }
于 2017-05-07T09:37:50.313 回答
1

您可以直接.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);
于 2020-03-24T09:26:52.333 回答
-1

对我来说就是这样

.setPriority(Notification.PRIORITY_HIGH);

这导致单击后通知不清除...确保您使用:

.setPriority(Notification.PRIORITY_DEFAULT);

并且.setAutoCancel(true)应该工作。

于 2018-02-11T17:33:46.747 回答