简体   繁体   English

Android前台服务通知不持久

[英]Android foreground service notification not persistent

I have created a class which extends Service and runs as a foreground service. 我创建了一个扩展Service的类,并作为前台服务运行。 I would like my service notification to be persistent (ie not removed by swiping). 我希望我的服务通知是持久的(即不能通过刷卡删除)。 However, my notification can be dismissed by swiping. 但是,我的通知可以通过滑动来解除。

The Service documentation states: ...A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. 服务文档说明:...前台服务必须提供状态栏的通知,该通知位于“正在进行”标题下。 This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground... 这意味着除非服务被停止或从前台移除,否则不能解除通知...

I did put break points to check whether onDestroy() or stopSelf() is hit, but this is not the case. 我确实设置了断点来检查onDestroy()或stopSelf()是否被击中,但事实并非如此。 The service is running in foreground mode, but I can dismiss the notification by swiping. 该服务正在前台模式下运行,但我可以通过滑动来关闭该通知。

I have found quite a few questions regarding the opposite case, where it was not possible to dismiss the notification after the service was stopped, but did not find any question similar to my problem. 我发现了很多关于相反情况的问题,在服务停止后无法解除通知,但没有发现任何类似于我的问题的问题。

The service is started via an Intent and initialized as follows: 该服务通过Intent启动并初始化如下:

@Override
public void onCreate()
{
    super.onCreate();

    initialize();
}

private void initialize()
{
    Notification n = get_service_notification();
    startForeground(10, n);

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(BroadcastCodes.service_broadcast_intent_name));
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    startServing();
    return START_STICKY;
}

where startServing() subscribes to location updates 其中startServing()订阅位置更新

The notification is built like this 通知是这样构建的

private Notification get_service_notification()
{
    Intent intent = new Intent(this, LoginActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder b = new NotificationCompat.Builder(this);

    b.setAutoCancel(false)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.app_icon)
            .setTicker("some text")
            .setContentTitle("some text")
            .setContentText("some text")
            .setContentIntent(contentIntent)
            .setContentInfo("")
            .setOngoing(true);

    Notification res = b.build();
    res.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;

    return res;
}

I have tried playing around with the flags and setOngoing/setAutoCancel, but the result stayed the same. 我尝试过使用标志和setOngoing / setAutoCancel,但结果保持不变。

SDK versions are configured as minSdkVersion 18, targetSdkVersion 25 SDK版本配置为minSdkVersion 18,targetSdkVersion 25

I would really appreciate any input regarding this issue - it might be just some stupid mistake, but I already did spend more than a few hours trying to research on my own...unfortunately to no avail. 我真的很感激有关这个问题的任何意见 - 这可能只是一些愚蠢的错误,但我已经花了超过几个小时试图自己研究......遗憾的是无济于事。

EDIT 1 编辑1

I still have not figured out the problem. 我还没弄明白这个问题。 I decided to try a dirty hack, but still no luck. 我决定尝试一个肮脏的黑客,但仍然没有运气。

I made a PendingIntent and registered it via setDeleteIntent() the idea being to display a new notification once this one was dismissed by swipe. 我创建了一个PendingIntent并通过setDeleteIntent()注册它,这个想法是在刷一下这个通知后显示一个新的通知。 Unfortunately, I was not able to get this to work (the intent never fires when swiping). 不幸的是,我无法使其工作(滑动时意图永远不会发生)。

I would also like to clarify, that the notification CAN BE swiped away, but IS NOT AFFECTED (ie is not deleted) by the clear-all button in the notification pane (the trash icon). 我还想澄清一下,通知可以通过通知窗格中的clear-all按钮(垃圾邮件图标)轻扫,但不会受到影响(即不会被删除)。

EDIT 2 编辑2

As mentioned above I was not able to use setDeleteIntent() to recreate my notification when it is dismissed by swipe. 如上所述,当刷卡解除时,我无法使用setDeleteIntent()重新创建通知。

For now I settled with a workaround - my service does some periodical tasks. 现在我解决了一个问题 - 我的服务做了一些定期任务。 I am now calling 我正在打电话

notificationManager.notify(10, service_notification);

when my task runs so that even if swiped away, my notification will be recreated after a while. 当我的任务运行时,即使被刷掉,我的通知也会在一段时间后重新创建。

I still have a strong feeling that I just misread the documentation, since there are several notifications on my test device (Honor API23) that can not be swiped away. 我仍然有一种强烈的感觉,我只是误读了文档,因为我的测试设备(荣誉API23)上有几个通知无法刷掉。

EDIT 3 编辑3

I have tried the same code on a different device (Lenovo API23) and the notification works as expected (can not be dismissed and can not be swiped). 我在不同的设备(Lenovo API23)上尝试了相同的代码,并且通知按预期工作(不能被解雇且无法刷卡)。 My problem seems to be device specific. 我的问题似乎是设备特定的。

I made a few further tests (besides the ones mentioned in updates). 我做了一些进一步的测试(除了更新中提到的那些)。 I tried running various examples/tutorials from around the internet like this page here 我试图运行来自各地的互联网各种例子/教程喜欢这个页面在这里

On the Honor I was initially running my tests the notification could always be swiped, on the Lenovo and others the "ongoing" flag works as stated in the documentation, ie it can not be dismissed via swipe action. 在Honor上我最初运行我的测试通知总是可以刷,在联想和其他人的“正在进行的”标志按照文档中的说明工作,即它不能通过滑动操作被解雇。

To sum it up, my problem really seems to be device specific. 总结一下,我的问题似乎确实是设备特定的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM