Android:当您的应用在后台运行时在通知上使用自动取消 [英] Android: Using AUTO-CANCEL on a notification when your app is running in the background

查看:21
本文介绍了Android:当您的应用在后台运行时在通知上使用自动取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里查看了所有其他 AUTO-CANCEL-not-working 问题,它们似乎都涉及我没有犯的错误.我都试过了

I have looked at all the other AUTO-CANCEL-not-working questions here, and they all seem to involve mistakes that I am not making. I have tried both

builder.setAutoCancel(true);

Notification notif = builder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;

两者都不起作用.

我正在使用 NotificationCompat,因为我的最低 API 是 8.这是我的完整代码.在此特定通知中,我没有调用意图,因为我不需要用户执行任何操作.

I am using NotificationCompat since my minimum API is 8. Here is my full code. In this particular notification, I am not calling an intent, since I don't need the user to do anything.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.app_name) + ": my title");
builder.setContentText(message);
builder.setSmallIcon(R.drawable.notification_icon);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prog_icon);
builder.setLargeIcon(bitmap);

builder.setAutoCancel(true); // dismiss notification on user click

NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notiManager.notify(MY_NOTI_MANAGER_ID, builder.build());

通知显示完美.您可以滑动以清除它.但简单地点击它不会解除通知.它只是亮起并留在那里.

The notification displays just perfectly. You can swipe to clear it. But simply tapping it does not dismiss the notification. It just lights up and stay there.

我的代码与此处发布的其他代码之间的一些可能差异:1)我正在使用 NotificationCompat (这应该没有什么区别,但我们以前听说过).2) 由于我的通知很简单,所以我没有附加意图.

Some possible differences between my code and others' posted here: 1) I am using NotificationCompat (which should not make a difference, but we've heard that before). 2) Since my notification is simple, I do not attach an intent.

如果您有任何见解,请告诉我.

Please let me know if you have any insights.

我的目的是关闭通知而不将我的后台应用程序置于前台.

My purpose is to dismiss a notification without foregrounding my background app.

推荐答案

显然你确实需要一个待定的意图.

So apparently you do need a pending intent.

Android - 通知管理器,有一个没有通知的通知Intent,我找到了一个解决方案,它可以将当前活动的应用程序作为您的待处理 Intent(这样您就不必开始自己的 Activity 来关闭通知).

At Android - notification manager, having a notification without an intent, I found a solution that grabs the current active application as your pending intent (so that you don't have to start your own activity in order to dismiss the notification).

我刚刚添加了以下两行代码(在设置自动取消之后):

I just added the following two lines of code (right after setting the auto-cancel):

PendingIntent notifyPIntent = 
    PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);     
builder.setContentIntent(notifyPIntent);

效果很好.我想说的是,如果您不希望您的活动因用户点击您的通知而重新启动,那么这是您的最佳选择.

It worked great. I would say that if you don't want your activity to restart as a result of the user clicking your notification, then this is your best option.

这篇关于Android:当您的应用在后台运行时在通知上使用自动取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆