class MainActivity : AppCompatActivity() {
lateinit var mContentViewBinding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mContentViewBinding=DataBindingUtil.setContentView(this, R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
importance)
mChannel.description = CHANNEL_DESC
mNotificationManager.createNotificationChannel(mChannel)
}
/*Click listener added.*/
mContentViewBinding.createNotificationBt.setOnClickListener {
createNotification()
}
}
private fun createNotification() {
//Pending intent notification Action button
val helpPendingIntent = PendingIntent.getBroadcast(
this@MainActivity,
REQUEST_CODE_ACTION,
Intent(this@MainActivity, NotificationReceiver::class.java)
.putExtra(
KEY_ACTION,
REQUEST_CODE_ACTION
),
PendingIntent.FLAG_UPDATE_CURRENT
)
//We need this object for getting direct input from notification
val remoteInput =
RemoteInput.Builder(NOTIFICATION_REPLY)
.setLabel(getString(R.string.reply_here))
.build()
//For the remote input we need this action object
val action = NotificationCompat.Action.Builder(
android.R.drawable.ic_dialog_email,
getString(R.string.reply), helpPendingIntent
)
.addRemoteInput(remoteInput)
.build()
//Creating the notification builder object
val mBuilder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.remote_message))
.setAutoCancel(true)
.setContentIntent(helpPendingIntent)
.addAction(action)
.addAction(android.R.drawable.ic_menu_directions, getString(R.string.action), helpPendingIntent)
//initializing the notification manager and showing the notification.
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID, mBuilder.build())
}
}
Be the first to comment.