9. ローカル通知 (AppsKitSDKLocalNotificationManager)
AKSには、独自のローカル通知を表示するための小規模なツールキットが付属しています。キュー、通知ビルダー、抽象クラスの BroadcastReceiver で構成されています。AKSは自身のマニフェストにレシーバーを登録せず、通知のスケジュール設定も行いません。どちらもアプリ側で実装する必要があります。
9.1 マニフェスト
AppsKitSDKLocalNotificationBroadCastReceiver を継承し(§9.2を参照)、作成したサブクラスを <receiver> として宣言して、§2と同じ <application> 要素内に追加します。
<application
android:name=".YourApplication">
<receiver
android:name=".YourNotificationReceiver"
android:exported="false" />
</application>Android 13以降(API 33)をターゲットにする場合は、通知の実行時権限も追加してください。AKS自身のマニフェストにはこの権限が宣言されていないため、自動的にはマージされません。
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />POST_NOTIFICATIONS は実行時権限のため、実際に通知を表示するには、事前にユーザーに権限をリクエストする必要があります(例:ActivityResultContracts.RequestPermission() を使用)。
9.2 通知をキューに追加する
AppsKitSDKLocalNotificationBroadCastReceiver を継承し、checkNotifications() を実装します。このメソッドはレシーバーが起動するたびに呼び出され、通知を表示するタイミングかどうかを判断するためのフックとして使用できます。
class YourNotificationReceiver : AppsKitSDKLocalNotificationBroadCastReceiver() {
override fun checkNotifications() {
addNotificationInQueue(
NotificationModel(
id = "daily_reminder", // dedup key — a second add with the same id is ignored
title = "Come back!",
description = "You have unfinished items waiting.",
icon = R.drawable.ic_notification,
notificationId = 1001, // the actual Android notification ID passed to notify()
targetScreen = "com.yourapp.MainActivity" // fully-qualified Activity class opened on tap
)
)
}
}checkNotifications() から戻った直後に、onReceive が自動的に AppsKitSDKLocalNotificationManager.showNotification(context) を呼び出します。このメソッドはキュー内で最も古い NotificationModel を取り出して表示します。このフローでは、showNotification を自分で呼び出す必要はありません。checkNotifications() 内でキューに追加するだけで十分です。
このキュー経由の表示処理では、Android 13以降で
POST_NOTIFICATIONSの権限を内部で確認し、許可されていない場合はユーザーに何も表示せずにスキップします(ログを1行出力します)。§9.1に従って、事前に権限をリクエストしてください。
9.3 スケジュール設定
AKSはこのレシーバーの定期実行を登録したり、起動したりしません。onReceive をいつ呼び出すかはアプリ側で決めます。通常は AlarmManager を使用します。
val intent = Intent(context, YourNotificationReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + intervalMs,
intervalMs,
pendingIntent
)intervalMs には独自の実行間隔を固定値で指定することも、AKSポータルのリモート設定で指定した値(HOURS_TO_MAKE_NOTIFICATION、未設定時のデフォルトは2時間)を使用することもできます。この値はレシーバーの基底クラスの getTimeIntervalInMilliSeconds() で取得できますが、アクセス修飾子が protected のため、上記の呼び出し箇所からではなく、サブクラス内から呼び出してください。
class YourNotificationReceiver : AppsKitSDKLocalNotificationBroadCastReceiver() {
fun intervalMillis(): Long = getTimeIntervalInMilliSeconds()
override fun checkNotifications() { /* ... */ }
}
AlarmManager.setRepeatingは、Dozeやバッテリー最適化の影響で実行時刻が不正確になります。アイドル状態でも確実に実行する必要がある場合は、実行のたびにsetExactAndAllowWhileIdleで次回の実行を設定するか、WorkManagerのPeriodicWorkRequest(最小実行間隔は15分)に切り替えてください。AKSはどちらの方式も指定していません。アプリに適した方式を選択してください。また、デバイスを再起動するとアラームは失われるため、アプリ側で再登録する必要があります(例:BOOT_COMPLETEDレシーバーから再登録)。
9.4 通知を直接表示する(キューを使用しない)
上記のキューとレシーバーを経由せずに、通知をすぐに表示するには、次のようにします。
// Tapping opens the Activity at classPath
AppsKitSDKLocalNotificationManager.showNotification(
context = this,
id = 2001,
classPath = "com.yourapp.MainActivity",
channelId = "general",
title = "New message",
message = "You've got a new message waiting.",
notificationIcon = R.drawable.ic_notification
)
// Or pass a fully-built Intent instead of a class path
AppsKitSDKLocalNotificationManager.showNotification(
context = this,
id = 2002,
intent = Intent(this, MainActivity::class.java).putExtra("from", "notification"),
channelId = "general",
title = "New message",
message = "Tap to view details.",
notificationIcon = R.drawable.ic_notification
)どちらのオーバーロードも、通知チャネルがまだ存在しない場合は自動的に作成します。§9.2のキュー経由の処理とは異なり、どちらも POST_NOTIFICATIONS の権限を内部では確認しません。Android 13以降では、アプリ側で実行時権限をリクエストしてください。権限がない場合、呼び出してもエラーが表示されず、通知が表示されないことがあります。