startForeground Notification does not show Title or Content
I found the issue:
You need to set a valid small icon. Otherwise the notification display will fail silently during startForeground.
val notification: Notification = NotificationCompat.Builder(applicationContext, MainActivity.RECORDING_NOTIFICATION_CHANNEL.id)
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("MyService")
.setContentText("Recording Service Ready")
.setContentIntent(pendingIntent)
.build()
I discovered this when trying to display the notification manually using NotificationManager.notify.
Alexander Jäger
Updated on December 20, 2022Comments
-
Alexander Jäger over 1 year
I'm starting a foreground service which uses a HandlerThread to do some long running tasks.
It works fine, however, the notification does not show my title or content (It says "xxx is running " Tap for more information or to stop the app"). I'm targeting Android 10 for now
The service is started from my main activity
Log.w(TAG, "startServiceCalled") Intent(this, MyService::class.java).also { intent -> startForegroundService(intent) }
In the
onCreate
callback of the Service I'm putting the service in the foreground like thisval pendingIntent: PendingIntent = Intent(this, MainActivity::class.java).let { notificationIntent -> PendingIntent.getActivity(this, 0, notificationIntent, 0) } val notification: Notification = NotificationCompat.Builder(applicationContext, MainActivity.RECORDING_NOTIFICATION_CHANNEL.id) .setOngoing(true) .setContentTitle("My Custom App") .setContentText("MyService Ready") .setContentIntent(pendingIntent) .build() startForeground(NOTIFICATIION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
BTW: I'm also using Flutter so my main activity extends FlutterActivity
EDIT: The notification channel is created like this
val RECORDING_NOTIFICATION_CHANNEL = NotificationChannel("com.example.notification", "Service notifications", NotificationManager.IMPORTANCE_DEFAULT).apply { description = "Provides information about the service" } val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(RECORDING_NOTIFICATION_CHANNEL);