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

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

问题描述

我已经看过了所有其他的AUTO-取消 - 不工作的问题在这里,他们似乎都涉及到,我不犯错误。我曾经尝试都

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;

无论是作品。

Neither works.

我使用NotificationCompat因为我最小的API为8。这里是我的全部code。在这个特殊的通知,我不打电话的意图,因为我不需要用户做任何事。

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.

之间我的code和他人张贴在这里的一些可能的不同: 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.

在<一个href="http://stackoverflow.com/questions/7040742/android-notification-manager-having-a-notification-without-an-intent">Android - 通知经理,有一个通知,没有意图,我发现了一个解决方案,抓住当前活动的应用程序作为待处理的意图(这样你就不必开始自己的活动,以驳回通知)。

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

我刚刚添加了下面两行code(设置自动取消后右):

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.

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

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