Showing posts with label 【ANDROID STUDIO】Tap Action Notification Kotlin. Show all posts
Showing posts with label 【ANDROID STUDIO】Tap Action Notification Kotlin. Show all posts

Saturday, June 19, 2021

【ANDROID STUDIO】Tap Action Notification Kotlin

 <?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="237dp"
tools:layout_editor_absoluteY="371dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.notificationdemo

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.NotificationCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
private val channelID = "com.example.notificationdemo.channel1"
private var notificationManager: NotificationManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createNotificationChannel(channelID,"DemoChannel","this is a demo")
button.setOnClickListener {
displayNotification()


}
}
private fun displayNotification() {
val notificationId = 45
val tapResultIntent = Intent(this, SecondActivity::class.java)
val pendingIntent: PendingIntent = PendingIntent.getActivity(
this,
0,
tapResultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
)

val notification = NotificationCompat.Builder(this@MainActivity, channelID)
.setContentTitle("Demo Title")
.setContentText("This is a demo notification")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
notificationManager?.notify(notificationId, notification)

}

private fun createNotificationChannel(id : String, name:String, channelDescription:String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_HIGH
val channel = NotificationChannel(id, name, importance).apply {
description = channelDescription
}
notificationManager?.createNotificationChannel(channel)

}

}

}

End of Summer Sale ☀️😎

20% OFF Inside!🤯 ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏  ͏...