I have a foreground service that shows an ongoing notification while running.
Now, it's a streaming app and I want the user to be notified when the stream gets broken (e.g. internet connection dropped). I can't use the main app because the stream can be going while other apps are active. So I need to send a notification to the user from the Foreground Service I use for streaming. The problem is, the notification is not being displayed.
Here's the code I'm currently using:
// registering notification channels
private fun createNotificationChannels() {
val serviceChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID_SERVICE,
NOTIFICATION_CHANNEL_NAME_SERVICE,
NotificationManager.IMPORTANCE_DEFAULT
)
val appChannel = NotificationChannel(
NOTIFICATION_CHANNEL_ID_APP,
NOTIFICATION_CHANNEL_NAME_APP,
NotificationManager.IMPORTANCE_HIGH
).apply {
enableVibration(true)
}
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannels(mutableListOf(serviceChannel, appChannel))
}
// starting the service with a required notification
startForeground(
nextInt(100000),
NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_SERVICE)
.setSmallIcon(R.drawable.recording_notification)
.setContentText("Stream is in progress...")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION
)
// letting the user know that stream crashed
private fun sendDisconnectNotification() {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID_APP)
.setSmallIcon(R.drawable.disconnected_notification)
.setContentTitle("The stream stopped unexpectedly!")
.setContentText("Please check your internet connection.")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
manager.notify(nextInt(100000), builder.build())
}
I know that sendDisconnectNotification() is being called (put logs there) but the notification never appears.
I was changing so many things that it's hard to specify every piece of code that I tried. But some of the important things I tried were changing the priorities for channels/notifications and sending the notifications in the same/different channels. I also uninstall the app and reboot the phone after every change to make sure the notification settings are applied.
Nothing has been working so far and it got me thinking it's impossible to do. I think that the foreground service only allows one notification to be displayed (the main ongoing one).
Can someone confirm this or give some advice on how to make it work? I can provide some more code samples if needed.
OK, I’m feeling kinda dumb but it turned out “Do Not Disturb” was turned on on the device. That was the reason the notifications were not visible. Writing this as an answer in case someone like me forgets to turn off DND and finds this SO question.