古語有云:書讀百遍,其義自現。是頗有必定道理的。編程
而在編程中,看過不如敲過,敲過不如理解,而不少代碼的精髓要義,都是在咱們寫過不少遍以後就天然而然的理解了。數組
iOS 中的推送分爲兩種:本地推送和遠程推送。與之對應兩種通知分別是LocalNotification
和RemoteNotification
。今天介紹本地推送,由於本地推送不涉及到蘋果的推送服務器,因此測試本地推送功能不須要開發者證書,甚至在模擬器上測試也能夠。bash
在App處於前臺時,建立一個或者多個本地通知,而後到了設置好的時間,iPhone 的通知中心,就會在指定時間將本地通知推送(分發)到App。服務器
咱們都知道點擊手機桌面的App圖標啓動時, -[AppDelegate application:didFinishLaunchingWithOptions:]
的第二個參數 launchOptions都爲nil。app
可是若是App已經被系統Kill掉或者被咱們本身徹底退出的狀況下,點擊通知時,也會啓動App,也會調用以下方法: -[AppDelegate application:didFinishLaunchingWithOptions:]
不一樣的是的launchOptions參數中能夠獲取本地通知的信息。 獲取本地通知的示例:ide
label.text = [launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description];
複製代碼
-[AppDelegate application:didFinishLaunchingWithOptions:]
,而是調用:- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification
{
NSLog(@"點擊了接收到了本地通知");
NSLog(@"%@",notification);
}
複製代碼
- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification
{
NSLog(@"點擊了接收到了本地通知");
NSLog(@"%@",notification);
// 咱們能夠在這裏作一些提醒。
}
複製代碼
首先,是註冊推送:測試
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
複製代碼
而後啓動App會看到以下圖所示的彈窗: ui
無論點擊OK仍是Don't Allow,都會進入以下方法:- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
// 本地推送,這個方法能夠不用寫,也不用設置。
}
複製代碼
而後,在控制器中建立一個本地通知,並調用:spa
UILocalNotification *localNotice = [UILocalNotification new];
localNotice.fireDate = [NSDate dateWithTimeIntervalSinceNow:5.0];
localNotice.alertBody = @"測試發了3條新消息";
localNotice.alertAction = @"鎖屏時的子標題";
localNotice.applicationIconBadgeNumber = 3;
self.localNotification = localNotice;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotice];
複製代碼
再而後,在AppDelegate 的兩個方法中處理本地通知: 完整示例:code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
if (launchOptions) {
/** 用一個label來顯示通知 */
UILabel *label = [[UILabel alloc]init];
label.backgroundColor = [UIColor redColor];
label.frame = CGRectMake(0, 100, 320, 200);
label.numberOfLines = 0;
label.text = [launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description];
label.font = [UIFont systemFontOfSize:11];
[self.window.rootViewController.view addSubview:label];
//清空角標
application.applicationIconBadgeNumber = 0;
}
return YES;
}
複製代碼
另外一個方法中的處理:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification
{
NSLog(@"點擊了接收到了本地通知");
NSLog(@"%@",notification);
application.applicationIconBadgeNumber = 0;
}
複製代碼
取消(刪除本地通知)
// 刪除某個本地通知
[[UIApplication sharedApplication] cancelLocalNotification:self.localNotification];
// 刪除全部的本地通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// 獲取將要執行的本地通知數組
NSArray *array = [UIApplication sharedApplication].scheduledLocalNotifications;
複製代碼
關於本地推送的屬性介紹和使用:這篇文章講的很是好