最近看唐巧的技術博客,有一篇博文提到如何改進客戶端升級提醒功能(原文在這裏: http://www.devtang.com/blog/2012/11/10/how-to-design-upgrade-notice/ ),採用的就是本地推送功能來提升用戶體驗。此外他還提到當時很火的一件事:一個技術男通過推送彈框向女友求愛。最簡單的實現方式也是採用本地推送UILocalNotification。於是我從網上也查了些資料,自己學習了下UILocalNotification,先對其進行一個總結。
(1)一般推送消息都是程序在後臺的狀態下出現:屏幕上方下滑出現提示,且app的badge數目加一。因此,在這種情況下,本地推送的創建代碼如下:
- (void)applicationDidEnterBackground:(UIApplication *)application { //創建實例 UILocalNotification *localNotification=[[UILocalNotification alloc] init]; if(localNotification) {//主要是判斷當前系統是否支持本地推送 //創建推送激發時間 NSDateFormatter *fm=[[NSDateFormatter alloc] init]; [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fireDate=[fm dateFromString:@"2015-8-7 09:49:00"]; //設置本地推送實例屬性 localNotification.fireDate=fireDate;//激發時間 localNotification.repeatInterval=NSCalendarUnitDay;//重複頻率 localNotification.timeZone=[NSTimeZone defaultTimeZone];//時區 localNotification.soundName=UILocalNotificationDefaultSoundName;//提醒聲音 localNotification.alertBody=@"This is the message body";//推送的消息體 localNotification.alertAction=@"OK打開應用";//鎖屏時,消息下方有“滑動來OK打開應用” localNotification.applicationIconBadgeNumber=1;//app圖標右上角的消息個數 //userInfo鍵值對用於區分不同的LocalNotification,從而可以刪除對應的localNotification NSDictionary *dic=@{@"name":@"Layne"}; localNotification.userInfo=dic; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } }
這樣一來,在app進入到後臺之後,在設定的時間就會彈出推送信息。
(2)還有一種情況,就是程序一直在前臺運行,這種情況下app也是會收到推送消息的,但是不會有提示,在appDelegate中有個專門的函數用於處理這種情況:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Title" message:@"This is push message!" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil]; [alert addAction:cancelAction]; [alert addAction:okAction]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; }
在這個函數裏我們可以定義AlertController進行專門的彈框提醒。
(3)最後,取消掉本地通知有兩種方式:
//取消所有的本地通知 [[UIApplication sharedApplication] cancelAllLocalNotifications]; //取消特定的本地通知 [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
(4)localNotification.userInfo的使用:
//獲取本地推送數組 NSArray *localArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; //聲明本地通知對象 UILocalNotification *localNoti; if(localArray){ for(UILocalNotification *noti in localArray){ NSDictionary *dict=noti.userInfo; if(dict){ if([dict["name"] isEqualToString:@"Layne"]){ //do something to the localnotification } break; } } }
即可通過userInfo找到對應的localNotification,從而進行一些操作(例如取消本地通知等)。
(5)自定義與推送通知的交互:
用到三個類:
UIUserNotificationAction(UIMutableUserNotificationAction): 定義具體的操作(按鈕)。
UIUserNotificationCategory(UIMutableUserNotificationCategory):包含一組UIUserNotificationAction
UIUserNotificationSettings:Notification的配置信息
具體代碼如下:
//定義第一個action(按鈕) UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];//第一個按鈕 action1.identifier = @"ACTION_1";//按鈕的標識 action1.title=@"Accept";//按鈕的標題 action1.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啓動程序 action1.authenticationRequired = NO;//不需要認證(解鎖) action1.destructive = NO; //定義第二個action(按鈕) UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕 action2.identifier = @"ACTION_2"; action2.title=@"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啓動程序,在後臺處理 action2.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; action2.destructive = YES; //定義一個category UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init]; category.identifier = @"LEI";//這組動作的唯一標示 [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault]; //定義settings UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithObjects:categorys, nil]]; //註冊settings [[UIApplication sharedApplication] registerUserNotificationSettings:uns]; 關鍵一步: //要把本地通知的category屬性設置爲和之前定義的category一樣,否則不會顯示按鈕 localNotification.category = @"LEI"; 最後: [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; ......
之後再appDelegate中會有對應的事件處理函數:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ }
注:appDelegate中有衆多的事件處理函數,例如:
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
這些事件處理函數可用來處理推送通知(本地推送和遠程推送)。
以上就是我對本地推送學習的所有總結,希望和大家一起學習進步。