[Android] Notification

leeehaยท2022๋…„ 11์›” 21์ผ
0
post-thumbnail

Notification์ด๋ž€?

  • ์ƒํƒœ๋ฐ”์— ์•ฑ์˜ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ์œ ์ €์—๊ฒŒ ์•Œ๋ฆผ์„ ๋ณด๋‚ด๋Š” ๊ธฐ๋Šฅ
  • ์ƒํƒœ๋ฐ”๋Š” ์‹œ์Šคํ…œ์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์•กํ‹ฐ๋น„ํ‹ฐ์—์„œ ์ง์ ‘ ์ œ์–ด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


Notification ์ƒ์„ฑ

  • NotificationChannel๋กœ NotificationCompat.Builder๋ฅผ ์ƒ์„ฑํ•˜๋ฉฐ, ์ด ๋นŒ๋”๋กœ Notification ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ์•Œ๋ฆผ์€ NotificationManager์˜ notify() ํ•จ์ˆ˜๋กœ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
    cf) NotificationCompat.Builder๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์ฑ„๋„ ๊ฐœ๋…์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ API Level 26 (Android 8) ๋ฒ„์ „๋ถ€ํ„ฐ ํ•ด๋‹น๋œ๋‹ค.


NotificationManager

Notification.Builder

  • Builder(context: Context!) - API Level 26 (Android 8) ์ด์ „ ๋ฒ„์ „
  • Builder(context: Context!, channelled: String!) - API Level 26 (Android 8) ๋ถ€ํ„ฐ
    • ์•ฑ์˜ ํ™˜๊ฒฝ์„ค์ •์—์„œ Notification์˜ ON/OFF๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐœ๋…์ด ๋ฐ”๋กœ '์ฑ„๋„'์ด๋‹ค.

NotificationChannel

  • NotificationChannel(id: String!, name: CharSequence!, importance: Int)
  • NotificationManager.IMPORTANCE_HIGH: ๊ธด๊ธ‰ ์ƒํ™ฉ & ์•Œ๋ฆผ์Œ์ด ์šธ๋ฆฌ๋ฉฐ ํ—ค๋“œ์—…์œผ๋กœ ํ‘œ์‹œ
  • NotificationManager.IMPORTANCE_DEFAULT: ๋†’์€ ์ค‘์š”๋„ & ์•Œ๋ฆผ์Œ์ด ์šธ๋ฆผ.
  • NotificationManager.IMPORTANCE_LOW: ์ค‘๊ฐ„ ์ค‘์š”๋„ & ์•Œ๋ฆผ์Œ์ด ์šธ๋ฆฌ์ง€ ์•Š์Œ.
  • NotificationManager.IMPORTANCE_MIN: ๋‚ฎ์€ ์ค‘์š”๋„ & ์•Œ๋ฆผ์Œ์ด ์—†๊ณ  ์ƒํƒœํ‘œ์‹œ์ค„์—๋„ ํ‘œ์‹œ๋˜์ง€ ์•Š์Œ.

Notification

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())
        }
    }
}


Notification ๊ตฌ์„ฑ

ํ„ฐ์น˜ ์ด๋ฒคํŠธ

  • static fun getActivity(context: Context!, requestCode: Int, intent: Intent!, flags: Int): PendingIntent!
  • static fun getBroadcast(context: Context!, requestCode: Int, intent: Intent!, flags: Int): PendingIntent!
  • static fun getService(context: Context!, requestCode: Int, intent: Intent, flags: Int): PendingIntent!
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()
)

Progress

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) 
    } 
}

style

  • BigPictureStyle, BigTextStyle, InboxStyle, Message Style

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())
        }
    }
}

profile
์Šต๊ด€์ด ๋  ๋•Œ๊นŒ์ง€ ๐Ÿ“

0๊ฐœ์˜ ๋Œ“๊ธ€