通過UILocalNotification 實現(xiàn)本地通知的推送
1 創(chuàng)建通知
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
notification.alertTitle = @"打開廣播";
notification.alertBody = @"廣播";
notification.repeatInterval = NSCalendarUnitSecond;
//設置本地通知的時區(qū)
notification.timeZone = [NSTimeZone defaultTimeZone];
//設置角標的個數(shù)
notification.applicationIconBadgeNumber=1;
notification.userInfo = @{@"info":@"broadcast"};
notification.soundName = UILocalNotificationDefaultSoundName;
2 取消通知
UIApplication *app = [UIApplication sharedApplication];
NSArray *array = [app scheduledLocalNotifications];
for (UILocalNotification * local in array) {
NSDictionary *dic = local.userInfo;
if ([dic[@"info"] isEqual: @"broadcast"]) {
//刪除指定的通知
[app cancelLocalNotification:local];
}
}
//也可以使用[app cancelAllLocalNotifications]刪除所有通知;
3 app跳出到后臺時,可在下列方法內(nèi)添加通知提示
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
4 app處于運行階段時
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//判斷應用程序當前的運行狀態(tài)是否為激活狀態(tài)
if (application.applicationState == UIApplicationStateActive) {
//只有激活狀態(tài) 在app運行時本地通知才有效
}
}