facebook twitter hatena line email

「Android/kotlin/Notification」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==ローカル通知== 通知が来るようになる。通知バーを見にいくと、新規通知が、追加されてることを確認。 AndoridManifest.xml <pre>...」)
 
(ローカル通知)
 
(同じ利用者による、間の1版が非表示)
行3: 行3:
  
 
AndoridManifest.xml
 
AndoridManifest.xml
 +
+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
 +
 +
 +
build.gradle
 +
+ implementation 'androidx.core:core:1.12.0'
 +
 +
MainActivity.kt から呼び出し
 
<pre>
 
<pre>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
+
NotificationUtil.sendNotification(this@MainActivity)
 
</pre>
 
</pre>
  
kotlin
+
NotificationUtil.kt
 
<pre>
 
<pre>
fun notification() {
+
package com.example.myapplication
    val notificationId = 1
+
    val channelId = "alarm_channel"
+
  
    // Android 13 以上の場合、通知権限の確認が必要
+
import android.Manifest
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
+
import android.app.NotificationChannel
        if (ActivityCompat.checkSelfPermission(
+
import android.app.NotificationManager
                this,
+
import android.content.Context
                Manifest.permission.POST_NOTIFICATIONS
+
import android.content.pm.PackageManager
            ) != PackageManager.PERMISSION_GRANTED
+
import android.os.Build
        ) {
+
import androidx.core.app.ActivityCompat
            // 権限がなければリクエスト
+
import androidx.core.app.NotificationCompat
            ActivityCompat.requestPermissions(
+
import androidx.core.app.NotificationManagerCompat
                this,
+
                arrayOf(Manifest.permission.POST_NOTIFICATIONS),
+
                1001 // リクエストコードは適当な値でOK
+
            )
+
            return // 権限が許可されるまで通知を表示しない
+
        }
+
    }
+
  
    // 通知チャネルの設定(Android 8.0以上)
+
class NotificationUtil {
    val notificationManager = NotificationManagerCompat.from(this)
+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+
        val channelName = "Alarm Channel"
+
        val channelDescription = "Channel for alarm notifications"
+
        val importance = NotificationManager.IMPORTANCE_HIGH // 通知の重要度をHIGHに設定
+
        val notificationChannel = NotificationChannel(channelId, channelName, importance).apply {
+
            description = channelDescription
+
        }
+
        notificationManager.createNotificationChannel(notificationChannel)
+
    }
+
  
     // 通知の作成
+
     companion object {
    val notification = NotificationCompat.Builder(this, channelId)
+
         fun sendNotification(context: Context) {
         .setSmallIcon(android.R.drawable.ic_notification_overlay) // 通知アイコン
+
            val notificationId = 1
        .setContentTitle("アラーム通知") // 通知タイトル
+
            val channelId = "alarm_channel"
        .setContentText("指定した時間が経過しました!") // 通知内容
+
        .setPriority(NotificationCompat.PRIORITY_HIGH) // 優先度をHIGHに設定
+
        .setDefaults(NotificationCompat.DEFAULT_ALL) // サウンド、振動などのデフォルト動作
+
        .build()
+
  
    // 通知を表示
+
            // Android 13 以上の場合、通知権限の確認が必要
    notificationManager.notify(notificationId, notification)
+
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
 +
                if (ActivityCompat.checkSelfPermission(
 +
                        context,
 +
                        Manifest.permission.POST_NOTIFICATIONS
 +
                    ) != PackageManager.PERMISSION_GRANTED
 +
                ) {
 +
                    // 権限がなければリクエスト
 +
                    ActivityCompat.requestPermissions(
 +
                        context as MainActivity,
 +
                        arrayOf(Manifest.permission.POST_NOTIFICATIONS),
 +
                        1001 // リクエストコードは適当な値でOK
 +
                    )
 +
                    return // 権限が許可されるまで通知を表示しない
 +
                }
 +
            }
 +
 
 +
            // 通知チャネルの設定(Android 8.0以上)
 +
            val notificationManager = NotificationManagerCompat.from(context)
 +
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
 +
                val channelName = "Alarm Channel"
 +
                val channelDescription = "Channel for alarm notifications"
 +
                val importance = NotificationManager.IMPORTANCE_HIGH // 通知の重要度をHIGHに設定
 +
                val notificationChannel = NotificationChannel(channelId, channelName, importance).apply {
 +
                    description = channelDescription
 +
                }
 +
                notificationManager.createNotificationChannel(notificationChannel)
 +
            }
 +
 
 +
            // 通知の作成
 +
            val notification = NotificationCompat.Builder(context, channelId)
 +
                .setSmallIcon(android.R.drawable.ic_notification_overlay) // 通知アイコン
 +
                .setContentTitle("アラーム通知") // 通知タイトル
 +
                .setContentText("指定した時間が経過しました!") // 通知内容
 +
                .setPriority(NotificationCompat.PRIORITY_HIGH) // 優先度をHIGHに設定
 +
                .setDefaults(NotificationCompat.DEFAULT_ALL) // サウンド、振動などのデフォルト動作
 +
                .build()
 +
 
 +
            // 通知を表示
 +
            notificationManager.notify(notificationId, notification)
 +
        }
 +
    }
 
}
 
}
 
</pre>
 
</pre>

2024年9月12日 (木) 01:23時点における最新版

ローカル通知

通知が来るようになる。通知バーを見にいくと、新規通知が、追加されてることを確認。

AndoridManifest.xml

+ <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />


build.gradle

+ implementation 'androidx.core:core:1.12.0'

MainActivity.kt から呼び出し

NotificationUtil.sendNotification(this@MainActivity)

NotificationUtil.kt

package com.example.myapplication

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat

class NotificationUtil {

    companion object {
        fun sendNotification(context: Context) {
            val notificationId = 1
            val channelId = "alarm_channel"

            // Android 13 以上の場合、通知権限の確認が必要
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                if (ActivityCompat.checkSelfPermission(
                        context,
                        Manifest.permission.POST_NOTIFICATIONS
                    ) != PackageManager.PERMISSION_GRANTED
                ) {
                    // 権限がなければリクエスト
                    ActivityCompat.requestPermissions(
                        context as MainActivity,
                        arrayOf(Manifest.permission.POST_NOTIFICATIONS),
                        1001 // リクエストコードは適当な値でOK
                    )
                    return // 権限が許可されるまで通知を表示しない
                }
            }

            // 通知チャネルの設定(Android 8.0以上)
            val notificationManager = NotificationManagerCompat.from(context)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channelName = "Alarm Channel"
                val channelDescription = "Channel for alarm notifications"
                val importance = NotificationManager.IMPORTANCE_HIGH // 通知の重要度をHIGHに設定
                val notificationChannel = NotificationChannel(channelId, channelName, importance).apply {
                    description = channelDescription
                }
                notificationManager.createNotificationChannel(notificationChannel)
            }

            // 通知の作成
            val notification = NotificationCompat.Builder(context, channelId)
                .setSmallIcon(android.R.drawable.ic_notification_overlay) // 通知アイコン
                .setContentTitle("アラーム通知") // 通知タイトル
                .setContentText("指定した時間が経過しました!") // 通知内容
                .setPriority(NotificationCompat.PRIORITY_HIGH) // 優先度をHIGHに設定
                .setDefaults(NotificationCompat.DEFAULT_ALL) // サウンド、振動などのデフォルト動作
                .build()

            // 通知を表示
            notificationManager.notify(notificationId, notification)
        }
    }
}