跳至主要內容
版本:v8

@capacitor/local-notifications

本地通知 API 提供了一種在本地排程裝置通知的方式(即無需伺服器發送推播通知)。

安裝

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 上,可以使用以下選項設定本地通知

屬性類型描述
smallIcon字串設定通知的預設狀態列圖示。圖示應放置在您應用程式的 res/drawable 資料夾中。此選項的值應為可繪製資源 ID,也就是不含副檔名的檔案名稱。僅適用於 Android。1.0.0
iconColor字串設定通知的預設狀態列圖示顏色。僅適用於 Android。1.0.0
sound字串設定通知的預設通知音效。在 Android 26+ 上,它會設定預設頻道音效,除非卸載應用程式,否則無法變更。如果找不到音訊檔案,則在 Android 21-25 上會播放預設系統音效,在 Android 26+ 上則不會播放音效。僅適用於 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 排程您的通知。請謹慎使用 allowWhileIdle,因為這些通知每個應用程式每 9 分鐘只能觸發一次。

API

schedule(...)

schedule(options: ScheduleOptions) => Promise<ScheduleResult>

排程一個或多個本地通知。

參數類型
optionsScheduleOptions

傳回:Promise<ScheduleResult>

1.0.0


getPending()

getPending() => Promise<PendingResult>

取得待處理通知的清單。

傳回:Promise<PendingResult>

1.0.0


registerActionTypes(...)

registerActionTypes(options: RegisterActionTypesOptions) => Promise<void>

註冊顯示通知時要採取的動作。

僅適用於 iOS 和 Android。

參數類型
optionsRegisterActionTypesOptions

1.0.0


cancel(...)

cancel(options: CancelOptions) => Promise<void>

取消待處理通知。

參數類型
optionsCancelOptions

1.0.0


areEnabled()

areEnabled() => Promise<EnabledResult>

檢查是否啟用通知。

傳回:Promise<EnabledResult>

1.0.0


getDeliveredNotifications()

getDeliveredNotifications() => Promise<DeliveredNotifications>

取得在通知畫面中可見的通知清單。

傳回:Promise<DeliveredNotifications>

4.0.0


removeDeliveredNotifications(...)

removeDeliveredNotifications(delivered: DeliveredNotifications) => Promise<void>

從通知畫面移除指定的通知。

參數類型
deliveredDeliveredNotifications

4.0.0


removeAllDeliveredNotifications()

removeAllDeliveredNotifications() => Promise<void>

從通知畫面移除所有通知。

4.0.0


createChannel(...)

createChannel(channel: Channel) => Promise<void>

建立通知頻道。

僅適用於 Android。

參數類型
channelChannel

1.0.0


deleteChannel(...)

deleteChannel(args: { id: string; }) => Promise<void>

刪除通知頻道。

僅適用於 Android。

參數類型
args{ id: string; }

1.0.0


listChannels()

listChannels() => Promise<ListChannelsResult>

取得通知頻道的清單。

僅適用於 Android。

傳回:Promise<ListChannelsResult>

1.0.0


checkPermissions()

checkPermissions() => Promise<PermissionStatus>

檢查是否具有顯示本地通知的權限。

傳回:Promise<PermissionStatus>

1.0.0


requestPermissions()

requestPermissions() => Promise<PermissionStatus>

請求顯示本地通知的權限。

傳回:Promise<PermissionStatus>

1.0.0


changeExactNotificationSetting()

changeExactNotificationSetting() => Promise<SettingsPermissionStatus>

將使用者導向應用程式設定畫面,以設定精確鬧鐘。

如果使用者將設定從已授權變更為已拒絕,應用程式將會重新啟動,並且任何使用精確鬧鐘排程的通知都將被刪除。

在 Android < 12 上,使用者不會被導向應用程式設定畫面,相反地,此函式將傳回 granted

僅適用於 Android。

傳回:Promise<SettingsPermissionStatus>

6.0.0


checkExactNotificationSetting()

checkExactNotificationSetting() => Promise<SettingsPermissionStatus>

檢查應用程式設定以使用精確鬧鐘。

僅適用於 Android。

傳回:Promise<SettingsPermissionStatus>

6.0.0


addListener('localNotificationReceived', ...)

addListener(eventName: 'localNotificationReceived', listenerFunc: (notification: LocalNotificationSchema) => void) => Promise<PluginListenerHandle>

監聽何時顯示通知。

參數類型
eventName'localNotificationReceived'
listenerFunc(notification: LocalNotificationSchema) => void

傳回:Promise<PluginListenerHandle>

1.0.0


addListener('localNotificationActionPerformed', ...)

addListener(eventName: 'localNotificationActionPerformed', listenerFunc: (notificationAction: ActionPerformed) => void) => Promise<PluginListenerHandle>

監聽在通知上執行動作的時間。

參數類型
eventName'localNotificationActionPerformed'
listenerFunc(notificationAction: ActionPerformed) => void

傳回:Promise<PluginListenerHandle>

1.0.0


removeAllListeners()

removeAllListeners() => Promise<void>

移除此外掛程式的所有監聽器。

1.0.0


介面

ScheduleResult

屬性類型描述
notificationsLocalNotificationDescriptor[]排程通知的列表。1.0.0

LocalNotificationDescriptor

描述本地通知的物件。

屬性類型描述
idnumber通知識別碼。1.0.0

ScheduleOptions

屬性類型描述
notificationsLocalNotificationSchema[]要排程的通知列表。1.0.0

LocalNotificationSchema

屬性類型描述
title字串通知的標題。1.0.0
body字串通知的內容,顯示在標題下方。1.0.0
largeBody字串設定在大型文字通知樣式中顯示的多行文字區塊。1.0.0
summaryText字串用於在收件匣和大型文字通知樣式中設定摘要文字詳細資訊。僅適用於 Android。1.0.0
idnumber通知識別碼。在 Android 上,它是一個 32 位元的整數。因此,該值應該介於 -2147483648 和 2147483647 之間(包含)。1.0.0
scheduleSchedule排程此通知以供稍後顯示。1.0.0
sound字串顯示此通知時播放的音訊檔案名稱。請包含檔案名稱的副檔名。在 iOS 上,該檔案應位於應用程式套件中。在 Android 上,該檔案應位於 res/raw 資料夾中。建議的格式為 .wav,因為 iOS 和 Android 都支援此格式。僅適用於 iOS 和 Android < 26。對於 Android 26+,請使用配置了所需聲音的頻道 ID。如果找不到聲音檔案(即,空字串或錯誤的名稱),則會使用預設的系統通知聲音。如果未提供,則會在 Android 上產生預設聲音,在 iOS 上則不會產生聲音。1.0.0
smallIcon字串設定自訂狀態列圖示。如果設定,則會覆寫 Capacitor 設定中的 smallIcon 選項。圖示應放置在應用程式的 res/drawable 資料夾中。此選項的值應為可繪製資源 ID,即不含副檔名的檔案名稱。僅適用於 Android。1.0.0
largeIcon字串設定通知的大型圖示。圖示應放置在應用程式的 res/drawable 資料夾中。此選項的值應為可繪製資源 ID,即不含副檔名的檔案名稱。僅適用於 Android。1.0.0
iconColor字串設定通知圖示的顏色。僅適用於 Android。1.0.0
attachmentsAttachment[]為此通知設定附件。1.0.0
actionTypeId字串將動作類型與此通知關聯。1.0.0
extraany設定要在此通知中儲存的額外資料。1.0.0
threadIdentifier字串用於群組多個通知。在 UNMutableNotificationContent 上設定 threadIdentifier。僅適用於 iOS。1.0.0
summaryArgument字串此通知新增至類別摘要格式字串的字串。在 UNMutableNotificationContent 上設定 summaryArgument。僅適用於 iOS。1.0.0
group字串用於群組多個通知。使用提供的值在 NotificationCompat.Builder 上呼叫 setGroup()。僅適用於 Android。1.0.0
groupSummaryboolean如果為 true,則此通知會成為一組通知的摘要。使用提供的值在 NotificationCompat.Builder 上呼叫 setGroupSummary()。僅在使用 group 時適用於 Android。1.0.0
channelId字串指定應在哪个頻道上傳遞通知。如果不存在具有指定名稱的頻道,則通知將不會觸發。如果未提供,則會使用預設頻道。使用提供的值在 NotificationCompat.Builder 上呼叫 setChannelId()。僅適用於 Android 26+。1.0.0
ongoingboolean如果為 true,則無法滑動關閉通知。使用提供的值在 NotificationCompat.Builder 上呼叫 setOngoing()。僅適用於 Android。1.0.0
autoCancelboolean如果為 true,則當使用者點擊通知時會取消通知。使用提供的值在 NotificationCompat.Builder 上呼叫 setAutoCancel()。僅適用於 Android。1.0.0
inboxListstring[]設定要在收件匣樣式通知中顯示的字串列表。最多允許 5 個字串。僅適用於 Android。1.0.0
silentboolean如果為 true,則在應用程式處於前景時通知不會出現。僅適用於 iOS。5.0.0

Schedule

表示通知的排程。

使用 atonevery 來排程通知。

屬性類型描述
atDate在特定日期和時間排程通知。1.0.0
repeatsbooleanat 指定的日期和時間重複傳遞此通知。僅適用於 iOS 和 Android。1.0.0
allowWhileIdleboolean允許此通知在 Doze 模式下觸發。僅適用於 Android 23+。請注意,這些通知只能在每個應用程式每 9 分鐘觸發一次。1.0.0
onScheduleOn在特定的間隔排程通知。這類似於排程 cron 工作。僅適用於 iOS 和 Android。1.0.0
everyScheduleEvery以特定間隔排程通知。1.0.0
countnumber限制 every 指定的間隔傳遞通知的次數。1.0.0

Date

啟用日期和時間的基本儲存和擷取。

MethodSignature描述
toString() => string傳回日期字串表示。字串的格式取決於地區設定。
toDateString() => string傳回日期字串值。
toTimeString() => string傳回時間字串值。
toLocaleString() => string傳回符合主機環境目前地區設定的字串值。
toLocaleDateString() => string傳回符合主機環境目前地區設定的日期字串值。
toLocaleTimeString() => string傳回符合主機環境目前地區設定的時間字串值。
valueOf() => number傳回自 1970 年 1 月 1 日午夜 (UTC) 以來的儲存時間值(以毫秒為單位)。
getTime() => number取得時間值(以毫秒為單位)。
getFullYear() => number使用本地時間取得年份。
getUTCFullYear() => number使用世界協調時間 (UTC) 取得年份。
getMonth() => number使用本地時間取得月份。
getUTCMonth() => number使用世界協調時間 (UTC) 取得 Date 物件的月份。
getDate() => number使用本地時間取得月份中的日期。
getUTCDate() => number使用世界協調時間 (UTC) 取得月份中的日期。
getDay() => number使用本地時間取得星期幾。
getUTCDay() => number使用世界協調時間 (UTC) 取得星期幾。
getHours() => number使用本地時間取得日期中的小時。
getUTCHours() => number使用世界協調時間 (UTC) 取得 Date 物件中的小時值。
getMinutes() => number使用本地時間取得 Date 物件的分鐘。
getUTCMinutes() => number使用世界協調時間 (UTC) 取得 Date 物件的分鐘。
getSeconds() => number使用本地時間取得 Date 物件的秒數。
getUTCSeconds() => number使用世界協調時間 (UTC) 取得 Date 物件的秒數。
getMilliseconds() => number使用本地時間取得 Date 的毫秒。
getUTCMilliseconds() => number使用世界協調時間 (UTC) 取得 Date 物件的毫秒。
getTimezoneOffset() => number取得本機電腦上的時間與世界協調時間 (UTC) 之間的分鐘差異。
setTime(time: number) => number設定 Date 物件中的日期和時間值。
setMilliseconds(ms: number) => number使用本地時間設定 Date 物件中的毫秒值。
setUTCMilliseconds(ms: number) => number使用世界協調時間 (UTC) 設定 Date 物件中的毫秒值。
setSeconds(sec: number, ms?: number | undefined) => number使用本地時間設定 Date 物件中的秒數值。
setUTCSeconds(sec: number, ms?: number | undefined) => number使用世界協調時間 (UTC) 設定 Date 物件中的秒數值。
setMinutes(min: number, sec?: number | undefined, ms?: number | undefined) => number使用本地時間設定 Date 物件中的分鐘值。
setUTCMinutes(min: number, sec?: number | undefined, ms?: number | undefined) => number使用世界協調時間 (UTC) 設定 Date 物件中的分鐘值。
setHours(hours: number, min?: number | undefined, sec?: number | undefined, ms?: number | undefined) => number使用本地時間設定 Date 物件中的小時值。
setUTCHours(hours: number, min?: number | undefined, sec?: number | undefined, ms?: number | undefined) => number使用世界協調時間 (UTC) 設定 Date 物件中的小時值。
setDate(date: number) => number使用本地時間設定 Date 物件的月份中的數值日期。
setUTCDate(date: number) => number使用世界協調時間 (UTC) 設定 Date 物件中的月份數值日期。
setMonth(month: number, date?: number | undefined) => number使用本地時間設定 Date 物件中的月份值。
setUTCMonth(month: number, date?: number | undefined) => number使用世界協調時間 (UTC) 設定 Date 物件中的月份值。
setFullYear(year: number, month?: number | undefined, date?: number | undefined) => number使用本地時間設定 Date 物件的年份。
setUTCFullYear(year: number, month?: number | undefined, date?: number | undefined) => number使用世界協調時間 (UTC) 設定 Date 物件中的年份值。
toUTCString() => string將日期轉換為使用世界協調時間 (UTC) 的字串並回傳。
toISOString() => string以 ISO 格式回傳日期字串值。
toJSON(key?: any) => string由 JSON.stringify 方法使用,以啟用物件資料的轉換,用於 JavaScript 物件表示法 (JSON) 序列化。

排程於

屬性類型
number
number
number
星期幾星期幾
number
number
number

附件

代表一個通知附件。

屬性類型描述
id字串附件識別符。1.0.0
網址字串附件的網址。使用 res 方案來參考網路資源,例如 res:///assets/img/icon.png。也接受 file 網址。1.0.0
options附件選項附件選項。1.0.0

附件選項

屬性類型描述
iosUNNotificationAttachmentOptionsTypeHintKey字串UNNotificationAttachment 的可雜湊選項中設定 UNNotificationAttachmentOptionsTypeHintKey 鍵。僅適用於 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailHiddenKey字串UNNotificationAttachment 的可雜湊選項中設定 UNNotificationAttachmentOptionsThumbnailHiddenKey 鍵。僅適用於 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailClippingRectKey字串UNNotificationAttachment 的可雜湊選項中設定 UNNotificationAttachmentOptionsThumbnailClippingRectKey 鍵。僅適用於 iOS。1.0.0
iosUNNotificationAttachmentOptionsThumbnailTimeKey字串UNNotificationAttachment 的可雜湊選項中設定 UNNotificationAttachmentOptionsThumbnailTimeKey 鍵。僅適用於 iOS。1.0.0

待處理結果

屬性類型描述
notifications待處理的本地通知架構[]待處理通知的清單。1.0.0

待處理的本地通知架構

屬性類型描述
title字串通知的標題。1.0.0
body字串通知的內容,顯示在標題下方。1.0.0
idnumber通知識別碼。1.0.0
scheduleSchedule排程此通知以供稍後顯示。1.0.0
extraany設定要在此通知中儲存的額外資料。1.0.0

註冊動作類型選項

屬性類型描述
類型動作類型[]要註冊的動作類型清單。1.0.0

動作類型

動作的集合。

屬性類型描述
id字串動作類型的 ID。在通知中透過 actionTypeId 鍵引用。1.0.0
動作動作[]與此動作類型相關聯的動作清單。1.0.0
iosHiddenPreviewsBodyPlaceholder字串設定 UNNotificationCategoryhiddenPreviewsBodyPlaceholder。僅適用於 iOS。1.0.0
iosCustomDismissActionbooleanUNNotificationCategory 的選項中設定 customDismissAction。僅適用於 iOS。1.0.0
iosAllowInCarPlaybooleanUNNotificationCategory 的選項中設定 allowInCarPlay。僅適用於 iOS。1.0.0
iosHiddenPreviewsShowTitlebooleanUNNotificationCategory 的選項中設定 hiddenPreviewsShowTitle。僅適用於 iOS。1.0.0
iosHiddenPreviewsShowSubtitlebooleanUNNotificationCategory 的選項中設定 hiddenPreviewsShowSubtitle。僅適用於 iOS。1.0.0

動作

當顯示通知時可以採取的動作。

屬性類型描述
id字串動作識別符。在 'actionPerformed' 事件中以 actionId 引用。1.0.0
title字串要為此動作顯示的標題文字。1.0.0
需要驗證booleanUNNotificationAction 的選項中設定 authenticationRequired。僅適用於 iOS。1.0.0
前景booleanUNNotificationAction 的選項中設定 foreground。僅適用於 iOS。1.0.0
破壞性booleanUNNotificationAction 的選項中設定 destructive。僅適用於 iOS。1.0.0
輸入boolean使用 UNTextInputNotificationAction 而不是 UNNotificationAction。僅適用於 iOS。1.0.0
輸入按鈕標題字串UNTextInputNotificationAction 上設定 textInputButtonTitle。僅當 inputtrue 時適用於 iOS。1.0.0
輸入預留位置字串UNTextInputNotificationAction 上設定 textInputPlaceholder。僅當 inputtrue 時適用於 iOS。1.0.0

取消選項

屬性類型描述
notificationsLocalNotificationDescriptor[]要取消的通知清單。1.0.0

已啟用結果

屬性類型描述
boolean裝置是否已啟用本地通知。1.0.0

已傳送的通知

屬性類型描述
notifications已傳送的通知架構[]通知畫面中可見的通知清單。1.0.0

已傳送的通知架構

屬性類型描述
idnumber通知識別碼。4.0.0
標籤字串通知標籤。僅適用於 Android。4.0.0
title字串通知的標題。4.0.0
body字串通知的內容,顯示在標題下方。4.0.0
group字串通知的已設定群組。僅適用於 Android。4.0.0
groupSummaryboolean此通知是否為通知群組的摘要。僅適用於 Android。4.0.0
資料any通知承載中包含的任何額外資料。僅適用於 Android。4.0.0
extraany要儲存在此通知中的額外資料。僅適用於 iOS。4.0.0
attachmentsAttachment[]此通知的附件。僅適用於 iOS。1.0.0
actionTypeId字串與此通知相關聯的動作類型。僅適用於 iOS。4.0.0
scheduleSchedule用於觸發此通知的排程。僅適用於 iOS。4.0.0
sound字串顯示通知時使用的聲音。僅適用於 iOS。4.0.0

頻道

屬性類型描述預設
id字串頻道識別符。1.0.0
名稱字串此頻道的人性化名稱 (呈現給使用者)。1.0.0
描述字串此頻道的描述 (呈現給使用者)。1.0.0
sound字串應為發佈至此頻道的通知播放的聲音。重要性至少為 3 的通知頻道應具有聲音。聲音檔案的檔名應相對於 Android 應用程式 res/raw 目錄指定。如果未提供聲音,或找不到聲音檔案,則不會使用聲音。1.0.0
重要性重要性發佈至此頻道的通知的中斷等級。31.0.0
可見性可見性發佈至此頻道的通知的可見性。此設定用於決定發佈至此頻道的通知是否顯示在鎖定畫面上,如果是,是否以編輯形式顯示。1.0.0
燈光boolean發佈至此頻道的通知是否應在支援的裝置上顯示通知燈光。1.0.0
燈光顏色字串發佈至此頻道的通知的燈光顏色。僅在啟用此頻道上的燈光且裝置支援時才支援。支援的顏色格式為 #RRGGBB#RRGGBBAA1.0.0
震動boolean發佈至此頻道的通知是否應震動。1.0.0

頻道清單結果

屬性類型描述
頻道頻道[]通知頻道清單。1.0.0

權限狀態

屬性類型描述
顯示權限狀態顯示通知的權限狀態。1.0.0

設定權限狀態

屬性類型描述
確切鬧鐘權限狀態使用確切鬧鐘的權限狀態。6.0.0

外掛程式監聽器處理

屬性類型
移除() => Promise<void>

已執行的動作

屬性類型描述
actionId字串已執行動作的識別符。1.0.0
輸入值字串使用者在通知中輸入的值。僅當通知的 input 設定為 true 時適用於 iOS。1.0.0
通知本地通知架構原始通知架構。1.0.0

類型別名

排程間隔

'year' | 'month' | 'two-weeks' | 'week' | 'day' | 'hour' | 'minute' | 'second'

重要性

重要性等級。如需更多詳細資訊,請參閱 Android 開發人員文件

1 | 2 | 3 | 4 | 5

可見性

通知可見性。如需更多詳細資訊,請參閱 Android 開發人員文件

-1 | 0 | 1

PermissionState

'prompt' | 'prompt-with-rationale' | 'granted' | 'denied'

列舉 (Enums)

Weekday

成員
星期日1
星期一2
星期二3
星期三4
星期四5
星期五6
星期六7