
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private val channelID = "com.bokchi.myapplication.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).apply { | |
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK | |
} | |
val pendingIntent:PendingIntent = PendingIntent.getActivity( | |
this, | |
0, | |
tapResultIntent, | |
PendingIntent.FLAG_UPDATE_CURRENT | |
) | |
//button1 | |
val intent2 = Intent(this, DetailActivity::class.java) | |
val pendingIntent2:PendingIntent = PendingIntent.getActivity( | |
this, | |
0, | |
intent2, | |
PendingIntent.FLAG_UPDATE_CURRENT | |
) | |
val action2 : NotificationCompat.Action = | |
NotificationCompat.Action.Builder(0, "Detail", pendingIntent2).build() | |
// action button2 | |
val intent3 = Intent(this, SettingActivity::class.java) | |
val pendingIntent3:PendingIntent = PendingIntent.getActivity( | |
this, | |
0, | |
intent2, | |
PendingIntent.FLAG_UPDATE_CURRENT | |
) | |
val action3 : NotificationCompat.Action = | |
NotificationCompat.Action.Builder(0, "Settings", pendingIntent3).build() | |
val notification = NotificationCompat.Builder(this@MainActivity, channelID) | |
.setContentTitle("Demo TItle") | |
.setContentText("This is a demo notifiation") | |
.setSmallIcon(android.R.drawable.ic_dialog_info) | |
.setAutoCancel(true) | |
.setPriority(NotificationCompat.PRIORITY_HIGH) | |
.setContentIntent(pendingIntent) | |
.addAction(action2) | |
.addAction(action3) | |
.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) | |
} | |
} | |
} |
'Android(Kotlin)' 카테고리의 다른 글
kotlin let (0) | 2021.04.26 |
---|---|
Andorid notification - 4 (0) | 2021.04.24 |
Andorid notification - 2 (0) | 2021.04.24 |
Andorid notification - 1 (0) | 2021.04.24 |
kotlin apply (0) | 2021.04.24 |