Java Android:当您的应用在后台运行时,在通知上使用 AUTO-CANCEL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16179899/
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
Android: Using AUTO-CANCEL on a notification when your app is running in the background
提问by BeccaP
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
我在这里查看了所有其他 AUTO-CANCEL-not-working 问题,它们似乎都涉及我没有犯的错误。我都试过了
builder.setAutoCancel(true);
and
和
Notification notif = builder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
Neither works.
两者都不起作用。
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,因为我的最低 API 是 8。这是我的完整代码。在这个特定的通知中,我没有调用意图,因为我不需要用户做任何事情。
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.
通知显示完美。您可以滑动以清除它。但简单地点击它并不会解除通知。它只是亮起并留在那里。
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.
我的代码和其他人在此处发布的代码之间可能存在一些差异:1)我使用的是 NotificationCompat(这应该没有区别,但我们以前听说过)。2)由于我的通知很简单,我没有附加意图。
Please let me know if you have any insights.
如果您有任何见解,请告诉我。
Edit: My purpose is to dismiss a notification without foregrounding my background app.
编辑:我的目的是关闭通知而不将我的后台应用程序置于前台。
采纳答案by BeccaP
So apparently you doneed a pending intent.
所以显然你确实需要一个待定的意图。
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).
在Android - 通知管理器中,有一个没有意图的通知,我找到了一个解决方案,它将当前活动的应用程序作为您的待处理意图(这样您就不必开始自己的活动来解除通知)。
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.
它工作得很好。我想说的是,如果您不希望您的活动因用户单击您的通知而重新启动,那么这是您的最佳选择。
回答by CommonsWare
You appear to be missing the PendingIntent
and setContentIntent()
call. I believe that is required for auto-cancel to work.
你似乎错过了PendingIntent
和setContentIntent()
电话。我相信这是自动取消工作所必需的。
Here is some Notification
-displaying logic from this sample projectthat works:
以下是此示例项目中的一些显示Notification
逻辑,该逻辑有效:
private void raiseNotification(Intent inbound, File output, Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis());
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), inbound.getType());
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
回答by Nibin
Hai dear friend if you want to show non cancelable notification(not cancelable for users) for a particular time and after that you need clear it (like the music player) you can use this.
亲爱的朋友,如果您想在特定时间显示不可取消的通知(用户不可取消),然后您需要清除它(如音乐播放器),您可以使用它。
mNotificationBuilder .setSmallIcon(android.R.drawable.btn_plus);
mNotificationBuilder .setContentTitle("My notification");
mNotificationBuilder .setContentText("Notificattion From service");
mNotificationBuilder .setLights(0xFF0000FF, 500, 500);
Notification note = mNotificationBuilder.build();
note.flags = Notification.FLAG_ONGOING_EVENT; // For Non cancellable notification
mNotificationManager.notify(NOTIFICATION_ID, note);