本地通知是 iOS 系統提供的提醒機制,常用於提醒、簽到或離開應用後的通知。
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
notification.alertBody = @"這是一則本地通知";
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
若 App 回到前台,可將角標歸零;若需要傳遞參數,可從 userInfo 中取出。