Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 1 year has passed since last update.

Notification

Posted at

Notification
Androidにおいて、Notification はユーザーにアプリからの通知を表示するための重要な仕組みです。

NotificationHelper クラスの作成:

①通知チャネルを作成するメソッド
②通知を表示するメソッド

①通知チャネルを作成するメソッド

// 通知チャネルを作成するメソッド
    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0以降の場合、通知チャネルを作成
            val channel = NotificationChannel(
                channelId,
                "Channel Name",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

細かく解説

val channel = NotificationChannel(
                channelId,
                "Channel Name",
                NotificationManager.IMPORTANCE_DEFAULT
            )

チャンネルの作成
channelId
チャンネルを識別するID(String)
"Channel Name",
チャンネル名
NotificationManager.IMPORTANCE_DEFAULT
通知の重要度

val notificationManager =  context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

↑context から通知サービスを取得する方法で、NotificationManager は通知を作成し、管理するためのクラスです。通知を作成したり、キャンセルしたりする際に使用します。

ただし、このキャストは as NotificationManager で行われています。これは getSystemService メソッドが Any 型を返すため、通知サービスであることを示すために明示的に NotificationManager にキャストしています。

notificationManager.createNotificationChannel(channel)

設定したチャンネルをnotificationManagerに登録

// 通知を表示するメソッド
    fun showNotification() {
        // 通知ビルダーを作成
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Notification Title")
            .setContentText("This is the notification content.")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        // 通知を表示
        with(NotificationManagerCompat.from(context)) {
            notify(notificationId, builder.build())
        }
    }
// 通知ビルダーを作成
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Notification Title")
            .setContentText("This is the notification content.")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

通知がどこのチャンネルに属するか設定
通知の内容を設定
通知の優先度を設定

 // 通知を表示
        with(NotificationManagerCompat.from(context)) {
            notify(notificationId, builder.build())
        }

指定された context から NotificationManagerCompat オブジェクトを取得します。
notify(notificationId, builder.build()): NotificationManagerCompat オブジェクトの notify メソッドを使用して通知を表示します。このメソッドには2つのパラメータがあります。第一引数は通知のIDで、これにより異なる通知を区別します。第二引数は Notification オブジェクトで、これは builder.build() で構築された通知です。

// 通知チャネルのID
    private val channelId = "channel_id"
    
    // 通知のID
    private val notificationId = 1

    // 通知チャネルを初期化するメソッド
    init {
        createNotificationChannel()
    }

    // 通知チャネルを作成するメソッド
    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Android 8.0以降の場合、通知チャネルを作成
            val channel = NotificationChannel(
                channelId,
                "Channel Name",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }
    }

    // 通知を表示するメソッド
    fun showNotification() {
        // 通知ビルダーを作成
        val builder = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Notification Title")
            .setContentText("This is the notification content.")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)

        // 通知を表示
        with(NotificationManagerCompat.from(context)) {
            notify(notificationId, builder.build())
        }
    }
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?