想要隐藏Notification,但没有获取Context.startForegroundService(),然后再调用Service.startForeground() [英] Want to hide Notification but getting Context.startForegroundService() did not then call Service.startForeground()

查看:289
本文介绍了想要隐藏Notification,但没有获取Context.startForegroundService(),然后再调用Service.startForeground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在后台运行服务时不显示通知.我知道根据Google Play规则,我们必须显示通知,但是对于我当前的应用程序,我不想显示任何通知.

I want to hide notification from appearing when the service is running in the background. I am aware that as per Google Play rules we have to show notification but for my current app i dont want to display any notification.

这是代码,可以正常工作并显示通知.但是如何使其不显示任何通知:

Here is the code, which works fine and shows notification. But how to make it not show any notification:

try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                String NOTIFICATION_CHANNEL_ID = "com.app..";
                String channelName = "App Background Service";
                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
                channel.setLightColor(Color.BLUE);
                channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                assert manager != null;
                manager.createNotificationChannel(channel);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
                Notification notification = notificationBuilder.setOngoing(true)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle("App is running in background")
                        .setPriority(NotificationManager.IMPORTANCE_MIN)
                        .setCategory(Notification.CATEGORY_SERVICE)
                        .build();
                startForeground(0, notification);
            } else
            {
                startForeground(0, new Notification());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

如果我不输入上面的代码,则会出现错误,提示未找到startForeground.并在5秒钟后自动被杀死.

If i dont put the above code it gives error saying no startForeground found. And gets killed after 5 sec automatically.

原因:Context.startForegroundService()然后未调用 Service.startForeground()

Reason: Context.startForegroundService() did not then call Service.startForeground()

我希望该服务不向用户显示任何通知,并在不被杀死的情况下静默完成其任务.在Build Version> 26中甚至可能吗?

I want the service not show any notification to user and finish its task silently without getting killed. Is that even possible in Build Version > 26?

推荐答案

我希望该服务不向用户显示任何通知,并且在不被杀死的情况下静默完成其任务

I want the service not show any notification to user and finish its task silently without getting killed

使用JobIntentService并确保您的工作将在10分钟内完成.请注意,工作开始之前可能会有一些延迟(这不会超出10分钟的限制).

Use JobIntentService and ensure that your work will get done in less than 10 minutes. Note that there may be some delay before the work begins (which will not count against that 10-minute limit).

或者,使用IntentService并确保您的工作将在不到1分钟的时间内完成.

Or, use IntentService and ensure that your work will get done in less than 1 minute.

这两个都不需要前台服务.

Neither of those requires a foreground service.

这篇关于想要隐藏Notification,但没有获取Context.startForegroundService(),然后再调用Service.startForeground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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