'android studio kotlin notification body click
Not sure how to open MainActivity when the body of notification is clicked:
class Notification : BroadcastReceiver()
{
override fun onReceive(context: Context, intent: Intent)
{
val notification = NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.icon_notif_white)
.setContentTitle(intent.getStringExtra(titleExtra))
.setContentText(intent.getStringExtra(messageExtra))
.build()
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(notificationID, notification)
}
}
Solution 1:[1]
You need to add setContentIntent
with pending intent for the MainActivity:
class Notification : BroadcastReceiver()
{
override fun onReceive(context: Context, intent: Intent)
{
val activityIntent = Intent(context, MainActivity::class.java)
val id = 0
val pendingIntent = PendingIntent.getActivity(context, id, activityIntent, PendingIntent.FLAG_CANCEL_CURRENT)
val notification = NotificationCompat.Builder(context, channelID)
.setSmallIcon(R.drawable.icon_notif_white)
.setContentTitle(intent.getStringExtra(titleExtra))
.setContentText(intent.getStringExtra(messageExtra))
.setContentIntent(pendingIntent)
.build()
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(notificationID, notification)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 0awawa0 |