本地通知不需要伺服器支援,由系統統一調度。下面從 AppDelegate 的完整生命週期切入,把註冊授權、建立通知、接收處理與取消的完整流程走一遍。
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 加入本地通知
[self addLocalNotification];
// 若 App 是由使用者點擊通知而啟動,可從 launchOptions 取出通知內容
UILocalNotification *notification = [launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSDictionary *userInfo = notification.userInfo;
NSLog(@"didFinishLaunchingWithOptions: The userInfo is %@.", userInfo);
return YES;
}
#pragma mark - 建立本地通知
- (void)addLocalNotification
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
// 觸發時間:10 秒後
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
// 重複間隔與參照日曆(使用前最好先設定時區,以便自動同步時間)
notification.repeatInterval = 2;
notification.repeatCalendar = [NSCalendar currentCalendar];
// 通知內容
notification.alertBody = @"最近添加了諸多有趣的特性,是否立即體驗?";
notification.applicationIconBadgeNumber = 1; // 圖示上的數字
notification.alertAction = @"打開應用"; // 鎖屏上的滑動提示文字
notification.alertLaunchImage = @"Default"; // 點擊通知開啟時的啟動圖
// 自訂音效(需真機才能聽到,且檔案要放在 main bundle)
notification.soundName = @"msg.caf";
// notification.soundName = UILocalNotificationDefaultSoundName;
// 附加資訊
notification.userInfo = @{@"id" : @1, @"user" : @"Kenshin Cui"};
// 排入通知佇列
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#pragma mark - 使用者回應授權請求後
// 呼叫 registerUserNotificationSettings: 之後會執行這個方法
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types != UIUserNotificationTypeNone)
{
[self addLocalNotification];
}
}
#pragma mark - 生命週期
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// 回到前台時清除圖示上的數字
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
#pragma mark - 移除通知
- (void)removeNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
#pragma mark - 接收通知
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
NSLog(@"didReceiveLocalNotification: The userInfo is %@", userInfo);
}
@end
原範例中有一段被條件編譯括起來的邏輯,值得提出來討論:
if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone)
{
[self addLocalNotification];
}
else
{
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert |
UIUserNotificationTypeBadge |
UIUserNotificationTypeSound
categories:nil]];
}
這個判斷的意思是:若尚未取得通知授權,就先請求授權,等使用者同意後再由 didRegisterUserNotificationSettings: 建立通知。這段邏輯本身是正確且值得參考的,但它有個重要前提——若從未發送過授權請求,系統的「設定」裡連通知的設定項目都不會出現,使用者也就無從手動開啟。因此首次啟動時主動請求授權是必要的。
同時也建議避免在啟動時立刻彈出授權視窗,比較好的做法是在使用者実際に觸發相關功能時(例如第一次設定提醒)再請求,通過率會高得多。
cancelLocalNotification: 移除指定通知,或用 cancelAllLocalNotifications 一次清空。實務上建議在 userInfo 中帶自訂識別碼,以便精準移除。