本地通知適合提醒、待辦或離線訊息。實作時可建立 UILocalNotification,並在前景收到通知時以彈窗呈現。
設定 fireDate、alertBody、alertAction、soundName 與 userInfo,再呼叫 presentLocalNotificationNow。
UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; notification.alertBody = @"提醒內容"; notification.soundName = @"sound.caf"; [application presentLocalNotificationNow:notification];
在 didReceiveLocalNotification 中可建立 UIAlertController 顯示內容。若 App 在前景收到遠端通知,也可判斷 applicationState 後轉成本地提醒。
若專案支援較新 iOS,建議改用 UserNotifications 框架的 UNNotificationRequest。