Android Jetpack - Notification


概要

  • アプリケーションがUIの外部に表示するメッセージ
  • タブ
  • は、通知を開くか、特定のタスク
  • を実行することができる.

    種類

  • ステータスバーにアイコンとして表示
  • 通知ウィンドウに表示
  • ヘッド・アラート-Android 5.0から
  • を提供
  • 画面をロック-Android 5.0から
  • アプリケーションアイコン培地-Android 8.0(API 26以降)から
  • を提供

    使用方法


  • 最も基本的なテキストのみを送信する目覚まし時計です.
    val channelID = "CHANNELID"
    
            val notificationChannel = NotificationChannel(
                channelID,
                "TestChannelName",
                NotificationManager.IMPORTANCE_DEFAULT
            )
    
            val builder = NotificationCompat.Builder(this, channelID)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setContentTitle("Title")
                .setContentText("ContentText")
    
            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)
    
            notificationManager.notify(
                1, // 해당 알림의 고유 ID
                builder // 표시할 알림
                    .setContentTitle("title")
                    .setContentText("content")
                    .build()
            )
  • アラームを管理するチャンネルを確立し、マネージャに登録します.
  • アラームビルダーを作成し、さまざまなプロパティを設定してから、()アラームを構築します.
  • マネージャにアラーム固有IDとアラームを送信し、通知()するとアラームが表示されます.

  • 「≪アラート|Alerts|emdw≫」をクリックすると、特定のアクティビティに移動し、アラートを削除します.
    val intent = Intent(this, NextActivity::class.java).apply {
                flags = Intent.FLAG_ACTIVITY_CLEAR_TASK // 앱 스택 관련처리 부분 굳이 없어도 작동은 함
            }
    
            val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    
            notificationManager.notify(
                1, // 해당 알림의 고유 ID
                builder // 표시할 알림
                    .setContentTitle("title")
                    .setContentIntent(pendingIntent)
                    .setContentText("content")
                    .setAutoCancel(true)
                    .build()
            )

  • 目覚まし時計にボタンを追加
  • は全部で3つのボタンを追加できます.
  • 「≪
  • タスク|
  • Tasks|Financial_Close≫」ボタンは、「≪アラート|Alerts|Financial_Close≫」タブの「≪実行するタスクの繰返し|Repeat To Execute Tasks|Financial_Close≫」
  • と一致しないでください.
  • BroadCastを使用してバックグラウンドでタスクを実行できます.
  • メディアを制御することもできる.リファレンス
  •  val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
      val action = NotificationCompat.Action.Builder(R.drawable.ic_launcher_foreground, "TestAction", pendingIntent).build()
    
            notificationManager.notify(
                1, // 해당 알림의 고유 ID
                builder // 표시할 알림
                    .setContentTitle("title")
                    .addAction(action)
                    .setContentText("content")
                    .setAutoCancel(true)
                    .build()
            )