Notification์ ์๋ฆผ์ ์ถ๋ ฅ๋ ์ด๋ฏธ์ง, ๋ฌธ์์ด ๋ฑ์ ์ ๋ณด๋ฅผ ๋ด๋ ๊ฐ์ฒด
builder.setSmallIcon(android.R.drawable.ic_notification_overlay)
builder.setWhen(System.currentTimeMillis())
builder.setContentTitle("Content Title")
builder.setContentText("Content Message")
package com.tutorial.c74
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Color
import android.media.AudioAttributes
import android.media.RingtoneManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val builder: NotificationCompat.Builder
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "one-channel"
val channelName = "My One Channel"
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH
)
channel.description = "My Channel One Description"
channel.setShowBadge(true)
val uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val audio = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM)
.build()
channel.setSound(uri, audio)
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
channel.vibrationPattern = longArrayOf(100, 200, 100, 200)
manager.createNotificationChannel(channel)
builder = NotificationCompat.Builder(this, channelId)
}else{
builder = NotificationCompat.Builder(this)
}
builder.setSmallIcon(android.R.drawable.ic_notification_overlay)
builder.setWhen(System.currentTimeMillis())
builder.setContentTitle("Title")
builder.setContentText("message")
manager.notify(1, builder.build())
}
}
}
val intent = Intent(this, DetailActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent)
Notification์ ์ต๋ 3๊ฐ๊น์ง์ ์ ์ ์ด๋ฒคํธ๋ฅผ ์ํ ์ก์ ์ ์ถ๊ฐํ ์ ์๋ค.
val actionIntent = Intent(this, DetailActivity::class.java)
val actionPendingIntent = PendingIntent.getActivity(this, 20, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.addAction(
NotificationCompat.Action.Builder(
android.R.drawable.stat_notify_more,
"Action",
actionPendingIntent
).build()
)
Notification์ผ๋ก ์์ ์ ์งํ ์ํฉ์ ํ์ธํ๊ณ ์ถ์ ๋
builder.setProgress(100, 0, false)
manager.notify(11, builder.build())
thread {
for (i in 1..100){
builder.setProgress(100, i, false)
manager.notify(11, builder.build())
SystemClock.sleep(100)
}
}
val bigPicture = BitmapFactory.decodeResource(resources, R.drawable.logo_1)
val bigStyle = NotificationCompat.BigPictureStyle()
bigStyle.bigPicture(bigPicture)
builder.setStyle(bigStyle)
package com.tutorial.c75
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
val builder: NotificationCompat.Builder
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channelId = "one"
val channelName = "channel"
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH
)
channel.description = "one description"
manager.createNotificationChannel(channel)
builder = NotificationCompat.Builder(this, channelId)
}else{
builder = NotificationCompat.Builder(this)
}
builder.setSmallIcon(android.R.drawable.ic_notification_overlay)
builder.setWhen(System.currentTimeMillis())
builder.setContentTitle("Title")
builder.setContentText("Text")
val actionIntent = Intent(this, DetailActivity::class.java)
val actionPending = PendingIntent.getActivity(this, 20,
actionIntent, PendingIntent.FLAG_CANCEL_CURRENT)
builder.setContentIntent(actionPending)
val actionPending2 = PendingIntent.getActivity(this, 20,
actionIntent, PendingIntent.FLAG_CANCEL_CURRENT)
builder.addAction(
NotificationCompat.Action.Builder(
android.R.drawable.stat_notify_more,
"Action",
actionPending2
).build()
)
val picture = BitmapFactory.decodeResource(resources, R.drawable.logo_1)
val style = NotificationCompat.BigPictureStyle()
style.bigPicture(picture)
builder.setStyle(style)
manager.notify(1, builder.build())
}
}
}