Android 单击操作后如何关闭通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11883534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:05:07  来源:igfitidea点击:

How to dismiss notification after action has been clicked

androidactionandroid-notifications

提问by endowzoner

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? I did not find anything.

任何提示?或者这还不是 API 的一部分?我没有找到任何东西。

回答by Kaediil

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. 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 或可能将 id 放入您添加到 PendingIntent 内的 Intent 的 Bundle 中?

回答by aaronvargas

Found this to be an issue when using Lollipop's Heads Up Display notification. See design guidelines. Here's the complete(ish) code to implement.

在使用 Lollipop 的抬头显示通知时发现这是一个问题。请参阅设计指南。这是要实现的完整(ish)代码。

Until now, having a 'Dismiss' button was less important, but now it's more in your face.

到目前为止,拥有“关闭”按钮并不重要,但现在更重要的是在您的脸上。

heads up notification

抬头通知

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>

回答by Vicki

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. This intermediary activity without any UI cancels the notification and then starts the activity I really wanted to start from the notification.

我最终创建了一个新的 NotificationActivity 来解决这些问题。这个没有任何 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

回答by Himanshu Khandelwal

In my opinion using a BroadcastReceiveris a cleaner way to cancel a Notification:

在我看来,使用 aBroadcastReceiver是一种更简洁的取消通知的方式:

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

回答by Malachiasz

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

回答by CommonsWare

You can always cancel()the Notificationfrom whatever is being invoked by the action (e.g., in onCreate()of the activity tied to the PendingIntentyou supply to addAction()).

您可以随时cancel()Notification从任何正在被调用的动作(例如,在onCreate()活动捆绑到的PendingIntent您提供的addAction())。

回答by Hanisha

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

回答by Houssin Boulla

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 相同

回答by rudakovsky

builder.setAutoCancel(true);

builder.setAutoCancel(true);

Tested on Android 9 also.

也在 Android 9 上进行了测试。