Android Front End Service Notification Not Displaying

I am trying to start the foreground service. I get a notification that the service is really starting, but the notification is always suppressed. I double-checked that the application can show notifications in the application information 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 that I see in relation: 06-20 12:26:43.635 895-930/? E/NotificationService: Suppressing notification from the package by user request. 06-20 12:26:43.635 895-930/? E/NotificationService: Suppressing notification from the package by user request.

+14
source share
7 answers

The problem was that I am using Android O, and this requires additional information. Here is a successful code for android O.

  mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager); mBuilder = new NotificationCompat.Builder(mActivity, "YOUR_TEXT_HERE").setSmallIcon(android.R.drawable.stat_sys_download).setColor (ContextCompat.getColor(mActivity, R.color.colorNotification)).setContentTitle(YOUR_TITLE_HERE).setContentText(YOUR_DESCRIPTION_HERE); mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build()); @TargetApi(26) private void createChannel(NotificationManager notificationManager) { String name = "FileDownload"; String description = "Notifications for download status"; int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel mChannel = new NotificationChannel(name, name, importance); mChannel.setDescription(description); mChannel.enableLights(true); mChannel.setLightColor(Color.BLUE); notificationManager.createNotificationChannel(mChannel); } 
+14
source

This is due to Android O bg service limitations.

So now you need to call startForeground() only for services that were started using startForegroundService() and call it the first 5 seconds after the service starts.

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 a notification (with a channel, as previously assumed):

 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 other notifications, even if you use different channel identifiers, so it can be hidden as a group with others. Use different groups to avoid this.

+21
source

If none of the above worked, check if your notification id is 0 ... SURPRISE !! it cannot be 0.

Thanks a lot @Luka Kama for this post

 startForeground(0, notification); // Doesn't work... startForeground(1, notification); // Works!!! 
+2
source

if you are FOREGROUND_SERVICE Android FOREGROUND_SERVICE 9 (Pie) level 28 or higher than you must give FOREGROUND_SERVICE permission in the manifest file. follow this link: https://developer.android.com/about/versions/pie/android-9.0-migration# BFA

+1
source

I ran into the same problem, and yet, the notification did not show after I followed all the suggestions. I had to uninstall the application from the device in order to install it again. (Samsung S9 +).

0
source

For me, everything was installed correctly (FOREGROUND_SERVICE permission was also added for the manifest), but I just needed to uninstall the application and reinstall it.

0
source

In my case, it was called by me using IntentService .

In short, if you want to use the foreground function, then a subclass of Service .

-2
source

Source: https://habr.com/ru/post/1269039/


All Articles