I am trying to start a foreground service. I get notified that the service does start but the notification always gets suppressed. I double checked that the app is allowed to show notifications in the app info on my device. Here is my code:
private void showNotification() { Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setAction(Constants.ACTION.MAIN_ACTION); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); Notification notification = new NotificationCompat.Builder(getApplicationContext()) .setContentTitle("Revel Is Running") .setTicker("Revel Is Running") .setContentText("Click to stop") .setSmallIcon(R.mipmap.ic_launcher) //.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)) .setContentIntent(pendingIntent) .setOngoing(true).build(); startForeground(Constants.FOREGROUND_SERVICE, notification); Log.e(TAG,"notification shown"); }
Here is the only error I see in relation: 06-20 12:26:43.635 895-930/? E/NotificationService: Suppressing notification from the package by user request.
Foreground services show a status bar notification, so that users are actively aware that your app is performing a task in the foreground and is consuming system resources. Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services.
Foreground services are an advanced Android concept which allows you to display notifications to your users when running long lived background tasks. The notification acts like any other notification, however it cannot be removed by the user and lives for the duration of the service.
To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager. notify() . To update this notification after you've issued it, update or create a NotificationCompat.
A Background Service is a service that runs only when the app is running so it'll get terminated when the app is terminated. A Foreground Service is a service that stays alive even when the app is terminated.
It's because of Android O bg services restrictions.
So now you need to call startForeground()
only for services that were started with startForegroundService()
and call it in first 5 seconds after service has been started.
Here is the guide - https://developer.android.com/about/versions/oreo/background#services
Like this:
//Start service: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(new Intent(this, YourService.class)); } else { startService(new Intent(this, YourService.class)); }
Then create and show notification (with channel as supposed earlier):
private void createAndShowForegroundNotification(Service yourService, int notificationId) { final NotificationCompat.Builder builder = getNotificationBuilder(yourService, "com.example.your_app.notification.CHANNEL_ID_FOREGROUND", // Channel id NotificationManagerCompat.IMPORTANCE_LOW); //Low importance prevent visual appearance for this notification channel on top builder.setOngoing(true) .setSmallIcon(R.drawable.small_icon) .setContentTitle(yourService.getString(R.string.title)) .setContentText(yourService.getString(R.string.content)); Notification notification = builder.build(); yourService.startForeground(notificationId, notification); if (notificationId != lastShownNotificationId) { // Cancel previous notification final NotificationManager nm = (NotificationManager) yourService.getSystemService(Activity.NOTIFICATION_SERVICE); nm.cancel(lastShownNotificationId); } lastShownNotificationId = notificationId; } public static NotificationCompat.Builder getNotificationBuilder(Context context, String channelId, int importance) { NotificationCompat.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { prepareChannel(context, channelId, importance); builder = new NotificationCompat.Builder(context, channelId); } else { builder = new NotificationCompat.Builder(context); } return builder; } @TargetApi(26) private static void prepareChannel(Context context, String id, int importance) { final String appName = context.getString(R.string.app_name); String description = context.getString(R.string.notifications_channel_description); final NotificationManager nm = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); if(nm != null) { NotificationChannel nChannel = nm.getNotificationChannel(id); if (nChannel == null) { nChannel = new NotificationChannel(id, appName, importance); nChannel.setDescription(description); nm.createNotificationChannel(nChannel); } } }
Remember that your foreground notification will have the same state as your other notifications even if you'll use different channel ids, so it might be hidden as a group with others. Use different groups to avoid it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With