在 Android 3.0 之前,通知是使用 Notification() 來達成
val notification = Notification(icon, tickerText, when)
在 Android 3.0 以後,使用 Notification.Builder 執行通知
val notification = Notification.Builder(context)
所以後來為了兼容之前的版本,新增了 NotificationCompat.Builder 來執行通知
val notification = NotificationCompat.Builder(context)
另外,在 Android API level 26.1.0 只傳入 context 的建構子已被棄用,新的建構子還需多傳入 channel Id
val notification = NotificationCompat.Builder(context, channelId)
要成功顯示出通知
如果在 Android 4.4 版本以上設定 setLargeIcon 時使用 BitmapFactory.decode 來提供 Bitmap 的話
傳入的資源檔如果是 vector ,則 bitmap 會回傳 null,造成圖案無法顯示;建議直接使用 jpeg 或 png 等圖檔
val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val builder =
    NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_star_black_24dp)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        
val notificationId = 1
val notification = builder.build()
notificationManager.notify(notificationId, notification)
在 Android 8.0 後,除了原本的動作還 "必須" 建立一個 NotificationChannel 並使用 createNotificationChannel() 方法指定給 NotificationManager
要注意在建立 Builder 時傳入的參數 "channelId" 要與建立的 NotificationChannel 物件帶入的第一個參數一致,這樣才能對應的上
val builder =
    NotificationCompat.Builder(this, "channel01")
        .setSmallIcon(R.drawable.ic_star_black_24dp)
        .setContentTitle("My notification")
        .setContentText("Hello World")
        .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
        .setVibrate(longArrayOf(300, 600, 300, 600))
        .setLights(Color.GREEN, 1000, 1000)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
val channel = NotificationChannel(
    "channel01",
    "MyChannel",
    NotificationManager.IMPORTANCE_HIGH
)
val notificationId = 1
val notification = builder.build()
notificationManager.createNotificationChannel(channel)
notificationManager.notify(notificationId, notification)
在按下通知欄的通知時,我們可能會希望可以跳轉到某一個 Activity
這時候可以使用 PendingIntent 來達成
val intent = Intent(this, MainActivity::class.java)
val pendingIntent =
    PendingIntent.getActivity(
        this,
        0,
        intent,
        PendingIntent.FLAG_UPDATE_CURRENT)
接著在 Builder 建立時呼叫 setContentIntent 方法
 val builder =
            NotificationCompat.Builder(this, "channel01")
                .setSmallIcon(R.drawable.ic_star_black_24dp)
                .setContentTitle("My notification")
                .setContentText("Hello World")
                
                ...
                
                .setContentIntent(pendingIntent)
完整專案可以看 Github 連結