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();
}
If i dont put the above code it gives error saying no startForeground found. And gets killed after 5 sec automatically.
Reason: Context.startForegroundService() did not then call Service.startForeground()
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?