this are the minSdk 29 and targetSdk 33
**This is the method for creating the notification. Am I missing any method that needed to be called? **
fun showNotification() {
val prevIntent =
Intent(baseContext, NotificationReceiver::class.java).setAction(Notification.PREVIOUS)
val prevPendingIntent =
PendingIntent.getBroadcast(baseContext, 0, prevIntent, PendingIntent.FLAG_IMMUTABLE )
val nextIntent =
Intent(baseContext, NotificationReceiver::class.java).setAction(Notification.NEXT)
val nextPendingIntent =
PendingIntent.getBroadcast(baseContext, 0, nextIntent, PendingIntent.FLAG_IMMUTABLE)
val playPauseIntent =
Intent(baseContext, NotificationReceiver::class.java).setAction(Notification.PLAY_PAUSE)
val playPausePendingIntent = PendingIntent.getBroadcast(
baseContext,
0,
playPauseIntent,
PendingIntent.FLAG_IMMUTABLE
)
val exitIntent =
Intent(baseContext, NotificationReceiver::class.java).setAction(Notification.EXIT)
val exitPendingIntent =
PendingIntent.getBroadcast(baseContext, 0, exitIntent, PendingIntent.FLAG_IMMUTABLE)
val fevIntent =
Intent(baseContext, NotificationReceiver::class.java).setAction(Notification.FEV)
val fevPendingIntent =
PendingIntent.getBroadcast(baseContext, 0, fevIntent, PendingIntent.FLAG_IMMUTABLE)
val notification = NotificationCompat.Builder(this, Notification.CHANNEL_ID)
.setSmallIcon(R.drawable.app_logo)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.app_logo))
.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()
.setMediaSession(mediaSession.sessionToken)
).setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentTitle(MusicFiles.getAllData()[MusicFiles.getCurrentPos()].Title)
.setContentText(MusicFiles.getAllData()[MusicFiles.getCurrentPos()].Artist)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOnlyAlertOnce(true)
.setSilent(true)
.addAction(R.drawable.empty_fev , Notification.FEV , fevPendingIntent)
.addAction(R.drawable.skip_previous , Notification.PREVIOUS , prevPendingIntent)
.addAction(R.drawable.play , Notification.PLAY_PAUSE , playPausePendingIntent)
.addAction(R.drawable.skip_next , Notification.NEXT , nextPendingIntent)
.addAction(R.drawable.cross , Notification.EXIT , exitPendingIntent)
.build()
startForeground(1, notification)
}
I had made BroadcastReceiver to
class NotificationReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when(intent?.action){
Notification.PREVIOUS ->{
Toast.makeText(context , "prevClicked" , Toast.LENGTH_SHORT).show()
}
Notification.NEXT ->{
Toast.makeText(context , "nextClicked" , Toast.LENGTH_SHORT).show()
}
Notification.PLAY_PAUSE ->{
Toast.makeText(context , "playPauseClicked" , Toast.LENGTH_SHORT).show()
}
Notification.EXIT ->{
Toast.makeText(context , "exitClicked" , Toast.LENGTH_SHORT).show()
}
}
}
}
Don't know the cause of this problem expecting to know the correct way of creating a media notification