安卓:在使用时通知您的应用程序在后台运行AUTO-取消您的、应用程序、后台、通知

由网友(你却无回眸)分享简介:我已经看过了所有其他的AUTO-取消 - 不工作的问题在这里,他们似乎都涉及到,我不犯错误。我曾经尝试都I have looked at all the other AUTO-CANCEL-not-working questions here, and they all seem to involve mistake...

我已经看过了所有其他的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.

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

阅读全文

相关推荐

最新文章