推送通知
• 推送通知是可見的(能用肉眼看到)
● iOS中提供了2種推送通知
-
● 本地推送通知(Local Notification)
-
● 遠程推送通知(Remote Notification)
● 推送通知的作用
● 在屏幕頂部顯示一塊橫幅(顯示具體內容)
● 在屏幕中間彈出一個UIAlertView(顯示具體內容)
● 在鎖屏界面顯示一塊橫幅(鎖屏狀態下,顯示具體內容)
● 播放音效(提醒作用)
● 發出推送通知時,如果程序正運行在前台,那么推送通知就不會被呈現出來
● 不管app打開還是關閉,推送通知都能如期發出
● 顧名思義,就是不需要聯網就能發出的推送通知(不需要服務器的支持)
● 本地推送通知的使用場景
● 常用來定時提醒用戶完成一些任務,比如
• 清理垃圾、記賬、買衣服、看電影、玩游戲
如何發出本地推送通知
UILocalNotification *ln = [[UILocalNotification alloc] init];
● 設置本地推送通知屬性
● 推送通知的觸發時間(何時發出推送通知)
@property(nonatomic,copy) NSDate *fireDate;
● 推送通知的具體內容
@property(nonatomic,copy) NSString *alertBody;
● 鎖屏界面顯示的小標題(完整小標題:“滑動來” + alertAction)
@property(nonatomic,copy) NSString *alertAction;
● 音效文件名
@property(nonatomic,copy) NSString *soundName;
● app圖標數字
@property(nonatomic) NSInteger applicationIconBadgeNumber;
● 獲得被調度的所有本地推送通知(等待發出的通知)
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
(已經發出且過期的推送通知就算調度結束,會自動從這個數組中移除)
● 取消調度本地推送通知
- (void)cancelLocalNotification:(UILocalNotification *)notification;
- (void)cancelAllLocalNotifications;
● 立即發出本地推送通知(使用價值:app在后台運行的時候)
- (void)presentLocalNotificationNow:(UILocalNotification *)notification;
本地推送通知的其他屬性
@property(nonatomic) NSCalendarUnit repeatInterval;
● 點擊推送通知打開app時顯示的啟動圖片
@property(nonatomic,copy) NSString *alertLaunchImage;
● 附加的額外信息
@property(nonatomic,copy) NSDictionary *userInfo;
● 時區
@property(nonatomic,copy) NSTimeZone *timeZone;
(一般設置為[NSTimeZone defaultTimeZone] ,跟隨手機的時區)
點擊本地推送通知
● app並沒有關閉,一直隱藏在后台
• 讓app進入前台,並會調用AppDelegate的下面方法(並非重新啟動app)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
● app已經被關閉(進程已死)
• 啟動app,啟動完畢會調用AppDelegate的下面方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
● launchOptions參數通過UIApplicationLaunchOptionsLocalNotificationKey 取出本地推送通知對象
實例:

#import "HMAppDelegate.h" #import "HMViewController.h" @interface HMAppDelegate() @property (nonatomic, weak) UILabel *label; @end @implementation HMAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // NSLog(@"------didFinishLaunchingWithOptions---%@"); UILabel *label = [[UILabel alloc] init]; label.backgroundColor = [UIColor redColor]; label.frame = CGRectMake(0, 100, 200, 100); label.font = [UIFont systemFontOfSize:11]; label.numberOfLines = 0; [[[self.window.rootViewController.childViewControllers firstObject] view] addSubview:label]; UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; if (note) { label.text = @"點擊本地通知啟動的程序"; HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject]; [homeVc performSegueWithIdentifier:@"home2detail" sender:note]; } else { label.text = @"直接點擊app圖標啟動的程序"; } self.label = label; return YES; } /** * 當用戶點擊本地通知進入app的時候調用 、通知發出的時候(app當時並沒有被關閉) */ - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { // self.label.text = [NSString stringWithFormat:@"點擊通知再次回到前台---%d", application.applicationState]; // 程序正處在前台運行,直接返回 if (application.applicationState == UIApplicationStateActive) return; HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject]; [homeVc performSegueWithIdentifier:@"home2detail" sender:notification]; } @end

#import "HMViewController.h" #import "HMDetailViewController.h" @interface HMViewController () - (IBAction)schedule; - (IBAction)cancel; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)schedule2 { // 1.創建本地推送通知對象 UILocalNotification *ln = [[UILocalNotification alloc] init]; // 2.設置通知屬性 // 音效文件名 ln.soundName = @"buyao.wav"; // 通知的具體內容 ln.alertBody = @"重大新聞:xxxx xxxx被調查了...."; // 鎖屏界面顯示的小標題("滑動來" + alertAction) ln.alertAction = @"查看新聞吧"; // 設置app圖標數字 ln.applicationIconBadgeNumber = 10; [[UIApplication sharedApplication] presentLocalNotificationNow:ln]; } - (IBAction)schedule { // 1.創建本地推送通知對象 UILocalNotification *ln = [[UILocalNotification alloc] init]; // 2.設置通知屬性 // 音效文件名 ln.soundName = @"buyao.wav"; // 通知的具體內容 ln.alertBody = @"重大新聞:xxxx xxxx被調查了...."; // 鎖屏界面顯示的小標題("滑動來" + alertAction) ln.alertAction = @"查看新聞吧"; // 通知第一次發出的時間(5秒后發出) ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; // 設置時區(跟隨手機的時區) ln.timeZone = [NSTimeZone defaultTimeZone]; // 設置app圖標數字 ln.applicationIconBadgeNumber = 5; // 設置通知的額外信息 ln.userInfo = @{ @"icon" : @"test.png", @"title" : @"重大新聞", @"time" : @"2014-08-14 11:19", @"body" : @"重大新聞:答復后即可更換就肯定會盡快趕快回家的瘋狂估計很快將發的" }; // 設置啟動圖片 ln.alertLaunchImage = @"Default"; // 設置重復發出通知的時間間隔 // ln.repeatInterval = NSCalendarUnitMinute; // 3.調度通知(啟動任務) [[UIApplication sharedApplication] scheduleLocalNotification:ln]; } - (IBAction)cancel { NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications; NSLog(@"%@", notes); // [[UIApplication sharedApplication] cancelAllLocalNotifications]; } - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note { HMDetailViewController *detailVc = segue.destinationViewController; detailVc.userInfo = note.userInfo; } @end