@capacitor/local-notifications
ローカル通知APIは、デバイスの通知をローカルにスケジューリングする方法を提供します(サーバーがプッシュ通知を送信しない場合)。
Install
npm install @capacitor/local-notifications
npx cap sync
Android
Android 13では、通知を送信するためにパーミッションチェックが必要です。checkPermissions()とrequestPermissions()を適切に呼び出す必要があります。
Android 12以前では、プロンプトは表示されず、許可されたものとして返されます。
Android 12以降、以下のパーミッションをAndroidManifest.xmlに追加しない限り、スケジュールされた通知は正確になりません:
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
パーミッションが存在していても、ユーザーはアプリ設定から正確な通知を無効にできることに注意してください。checkExactNotificationSetting()を使用して設定の値を確認してください。ユーザーがこの設定を無効にすると、アプリは再起動し、正確なアラームでスケジュールされた通知は削除されます。アプリケーションが正確なアラームに依存している場合は、フォールバックまたは代替動作を提供するために、アプリ起動時(例:App.appStateChange)にこの設定を確認してください。
Android 14では、USE_EXACT_ALARMという新しいパーミッションがあります。このパーミッションを使用すると、ユーザーにパーミッションを要求せずに正確なアラームを使用できます。これは、正確なアラームの使用がアプリの機能の中心で ある場合にのみ使用してください。このパーミッションの使用の影響についてはこちらを参照してください。
Android 15以降、ユーザーはプライベートスペースにアプリをインストールできます。ユーザーはいつでもプライベートスペースをロックでき、ユーザーがロックを解除するまでプッシュ通知は表示されません。
アプリがプライベートスペースにインストールされているかどうかを検出することはできません。そのため、アプリが重要な通知を表示する場合は、プライベートスペースにアプリをインストールしないようユーザーに通知してください。
プライベートスペースに関連するアプリの動作変更の詳細については、Androidドキュメントを参照してください。
設定
Androidでは、ローカル通知は以下のオプションで設定できます:
| プロパティ | 型 | 説明 | Since |
|---|---|---|---|
smallIcon | string | 通知のデフォルトのステータスバーアイコンを設定します。アイコンはアプリのres/drawableフォルダに配置する必要があ ります。このオプションの値は、拡張子なしのファイル名であるdrawableリソースIDです。Androidのみで使用可能です。 | 1.0.0 |
iconColor | string | 通知のステータスバーアイコンのデフォルトの色を設定します。Androidのみで使用可能です。 | 1.0.0 |
sound | string | 通知のデフォルトの通知音を設定します。Android 8以降ではデフォルトのチャンネルサウンドを設定し、アプリがアンインストールされない限り変更できません。オーディオファイルが見つからない場合、Android 7.xではデフォルトのシステムサウンドが再生され、Android 8以降では音が鳴りません。Androidのみで使用可能です。 | 1.0.0 |
設定例
capacitor.config.jsonでの設定:
{
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}
}
capacitor.config.tsでの設定:
/// <reference types="@capacitor/local-notifications" />
import { CapacitorConfig } from '@capacitor/cli';
const config: CapacitorConfig = {
plugins: {
LocalNotifications: {
smallIcon: "ic_stat_icon_config_sample",
iconColor: "#488AFF",
sound: "beep.wav",
},
},
};
export default config;
Doze
デバイスがDozeモードに入った場合、アプリケーションの機能が制限される可能性があります。Doze中でも通知を発火させる必要がある場合は、allowWhileIdle: trueを使用して通知をスケジュールしてください。これらの通知はアプリごとに9分に1回しか発火できないため、allowWhileIdleは慎重に使用してください。
API
schedule(...)getPending()registerActionTypes(...)cancel(...)areEnabled()getDeliveredNotifications()removeDeliveredNotifications(...)removeAllDeliveredNotifications()createChannel(...)deleteChannel(...)listChannels()checkPermissions()requestPermissions()changeExactNotificationSetting()checkExactNotificationSetting()addListener('localNotificationReceived', ...)addListener('localNotificationActionPerformed', ...)removeAllListeners()- Interfaces
- Type Aliases
- Enums
schedule(...)
schedule(options: ScheduleOptions) => Promise<ScheduleResult>
Schedule one or more local notifications.
| Param | Type |
|---|---|
options | |
Returns:
Promise<ScheduleResult>
Since: 1.0.0
getPending()
getPending() => Promise<PendingResult>
Get a list of pending notifications.
Returns:
Promise<PendingResult>
Since: 1.0.0
registerActionTypes(...)
registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>
Register actions to take when notifications are displayed.
Only available for iOS and Android.
| Param | Type |
|---|---|
options | |
Since: 1.0.0
cancel(...)
cancel(options: CancelOptions) => Promise<void>
Cancel pending notifications.
| Param | Type |
|---|---|
options | |
Since: 1.0.0
areEnabled()
areEnabled() => Promise<EnabledResult>