简体   繁体   English

单击操作后如何关闭通知

[英]How to dismiss notification after action has been clicked

Since API level 16 (Jelly Bean), there is the possibility to add actions to a notification with从 API 级别 16 (Jelly Bean) 开始,可以将操作添加到通知中

builder.addAction(iconId, title, intent);

But when I add an action to a notification and the action is pressed, the notification is not going to be dismissed.但是当我向通知添加一个动作并按下该动作时,该通知不会被解除。 When the notification itself is being clicked, it can be dismissed with当通知本身被点击时,它可以被关闭

notification.flags = Notification.FLAG_AUTO_CANCEL;

or要么

builder.setAutoCancel(true);

But obviously, this has nothing to with the actions associated to the notification.但显然,这与与通知相关的操作无关。

Any hints?任何提示? Or is this not part of the API yet?或者这还不是 API 的一部分? I did not find anything.我没有找到任何东西。

When you called notify on the notification manager you gave it an id - that is the unique id you can use to access it later (this is from the notification manager:当您在通知管理器上调用 notify 时,您给了它一个 id - 这是您以后可以用来访问它的唯一 id(这是来自通知管理器:

notify(int id, Notification notification)

To cancel, you would call:要取消,您可以调用:

cancel(int id)

with the same id.具有相同的 ID。 So, basically, you need to keep track of the id or possibly put the id into a Bundle you add to the Intent inside the PendingIntent?因此,基本上,您需要跟踪 id 或可能将 id 放入您添加到 PendingIntent 内的 Intent 的 Bundle 中?

Found this to be an issue when using Lollipop's Heads Up Display notification.在使用 Lollipop 的抬头显示通知时发现这是一个问题。 Seedesign guidelines .请参阅设计指南 Here's the complete(ish) code to implement.这是要实现的完整(ish)代码。

Until now, having a 'Dismiss' button was less important, but now it's more in your face.到目前为止,拥有“关闭”按钮并不重要,但现在更重要的是在您的脸上。

抬头通知

Building the Notification构建通知

int notificationId = new Random().nextInt(); // just use a counter in some util class...
PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notificationId, context);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setPriority(NotificationCompat.PRIORITY_MAX) //HIGH, MAX, FULL_SCREEN and setDefaults(Notification.DEFAULT_ALL) will make it a Heads Up Display Style
        .setDefaults(Notification.DEFAULT_ALL) // also requires VIBRATE permission
        .setSmallIcon(R.drawable.ic_action_refresh) // Required!
        .setContentTitle("Message from test")
        .setContentText("message")
        .setAutoCancel(true)
        .addAction(R.drawable.ic_action_cancel, "Dismiss", dismissIntent)
        .addAction(R.drawable.ic_action_boom, "Action!", someOtherPendingIntent);

// Gets an instance of the NotificationManager service
NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

// Builds the notification and issues it.
notifyMgr.notify(notificationId, builder.build());

NotificationActivity通知活动

public class NotificationActivity extends Activity {

    public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
        finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
    }

    public static PendingIntent getDismissIntent(int notificationId, Context context) {
        Intent intent = new Intent(context, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.putExtra(NOTIFICATION_ID, notificationId);
        PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        return dismissIntent;
    }

}

AndroidManifest.xml (attributes required to prevent SystemUI from focusing to a back stack) AndroidManifest.xml (防止 SystemUI 聚焦到返回堆栈所需的属性)

<activity
    android:name=".NotificationActivity"
    android:taskAffinity=""
    android:excludeFromRecents="true">
</activity>

I found that when you use the action buttons in expanded notifications, you have to write extra code and you are more constrained.我发现当你在扩展通知中使用操作按钮时,你必须编写额外的代码并且你受到更多限制。

You have to manually cancel your notification when the user clicks an action button.当用户单击操作按钮时,您必须手动取消通知。 The notification is only cancelled automatically for the default action.仅针对默认操作自动取消通知。

Also if you start a broadcast receiver from the button, the notification drawer doesn't close.此外,如果您从按钮启动广播接收器,通知抽屉不会关闭。

I ended up creating a new NotificationActivity to address these issues.我最终创建了一个新的 NotificationActivity 来解决这些问题。 This intermediary activity without any UI cancels the notification and then starts the activity I really wanted to start from the notification.这个没有任何 UI 的中间活动取消通知,然后从通知开始我真正想开始的活动。

I've posted sample code in a related post Clicking Android Notification Actions does not close Notification drawer .我在相关帖子中发布了示例代码Clicking Android Notification Actions does not close Notification drawer

In my opinion using a BroadcastReceiver is a cleaner way to cancel a Notification:在我看来,使用BroadcastReceiver是一种更简洁的取消通知的方式:

In AndroidManifest.xml:在 AndroidManifest.xml 中:

<receiver 
    android:name=.NotificationCancelReceiver" >
    <intent-filter android:priority="999" >
         <action android:name="com.example.cancel" />
    </intent-filter>
</receiver>

In java File:在java文件中:

Intent cancel = new Intent("com.example.cancel");
PendingIntent cancelP = PendingIntent.getBroadcast(context, 0, cancel, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Action actions[] = new NotificationCompat.Action[1];

NotificationCancelReceiver通知取消接收器

public class NotificationCancelReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        //Cancel your ongoing Notification
    };
}

您始终可以从操作调用的任何内容中cancel() Notification (例如,在与您提供给addAction()PendingIntent相关联的活动的onCreate()中)。

In new APIs don't forget about TAG:在新的 API 中不要忘记 TAG:

notify(String tag, int id, Notification notification)

and correspondingly并相应地

cancel(String tag, int id) 

instead of:代替:

cancel(int id)

https://developer.android.com/reference/android/app/NotificationManager https://developer.android.com/reference/android/app/NotificationManager

You will need to run the following code after your intent is fired to remove the notification.在您的意图被触发以删除通知后,您将需要运行以下代码。

NotificationManagerCompat.from(this).cancel(null, notificationId);

NB: notificationId is the same id passed to run your notification注意:notificationId 与传递给运行通知的 ID 相同

Just put this line :只要把这一行:

 builder.setAutoCancel(true);

And the full code is :完整的代码是:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.co.in/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.misti_ic));
    builder.setContentTitle("Notifications Title");
    builder.setContentText("Your notification content here.");
    builder.setSubText("Tap to view the website.");
    Toast.makeText(getApplicationContext(), "The notification has been created!!", Toast.LENGTH_LONG).show();

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    builder.setAutoCancel(true);
    // Will display the notification in the notification bar
    notificationManager.notify(1, builder.build());

builder.setAutoCancel(true); builder.setAutoCancel(true);

Tested on Android 9 also.也在 Android 9 上进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM