您好,登錄后才能下訂單哦!
最近看唐巧的技術(shù)博客,有一篇博文提到如何改進(jìn)客戶端升級提醒功能(原文在這里: http://www.devtang.com/blog/2012/11/10/how-to-design-upgrade-notice/ ),采用的就是本地推送功能來提升用戶體驗。此外他還提到當(dāng)時很火的一件事:一個技術(shù)男通過推送彈框向女友求愛。最簡單的實(shí)現(xiàn)方式也是采用本地推送UILocalNotification。于是我從網(wǎng)上也查了些資料,自己學(xué)習(xí)了下UILocalNotification,先對其進(jìn)行一個總結(jié)。
(1)一般推送消息都是程序在后臺的狀態(tài)下出現(xiàn):屏幕上方下滑出現(xiàn)提示,且app的badge數(shù)目加一。因此,在這種情況下,本地推送的創(chuàng)建代碼如下:
- (void)applicationDidEnterBackground:(UIApplication *)application { //創(chuàng)建實(shí)例 UILocalNotification *localNotification=[[UILocalNotification alloc] init]; if(localNotification) {//主要是判斷當(dāng)前系統(tǒng)是否支持本地推送 //創(chuàng)建推送激發(fā)時間 NSDateFormatter *fm=[[NSDateFormatter alloc] init]; [fm setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSDate *fireDate=[fm dateFromString:@"2015-8-7 09:49:00"]; //設(shè)置本地推送實(shí)例屬性 localNotification.fireDate=fireDate;//激發(fā)時間 localNotification.repeatInterval=NSCalendarUnitDay;//重復(fù)頻率 localNotification.timeZone=[NSTimeZone defaultTimeZone];//時區(qū) localNotification.soundName=UILocalNotificationDefaultSoundName;//提醒聲音 localNotification.alertBody=@"This is the message body";//推送的消息體 localNotification.alertAction=@"OK打開應(yīng)用";//鎖屏?xí)r,消息下方有“滑動來OK打開應(yīng)用” localNotification.applicationIconBadgeNumber=1;//app圖標(biāo)右上角的消息個數(shù) //userInfo鍵值對用于區(qū)分不同的LocalNotification,從而可以刪除對應(yīng)的localNotification NSDictionary *dic=@{@"name":@"Layne"}; localNotification.userInfo=dic; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; } }
這樣一來,在app進(jìn)入到后臺之后,在設(shè)定的時間就會彈出推送信息。
(2)還有一種情況,就是程序一直在前臺運(yùn)行,這種情況下app也是會收到推送消息的,但是不會有提示,在appDelegate中有個專門的函數(shù)用于處理這種情況:
-(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]; }
在這個函數(shù)里我們可以定義AlertController進(jìn)行專門的彈框提醒。
(3)最后,取消掉本地通知有兩種方式:
//取消所有的本地通知 [[UIApplication sharedApplication] cancelAllLocalNotifications]; //取消特定的本地通知 [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
(4)localNotification.userInfo的使用:
//獲取本地推送數(shù)組 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找到對應(yīng)的localNotification,從而進(jìn)行一些操作(例如取消本地通知等)。
(5)自定義與推送通知的交互:
用到三個類:
UIUserNotificationAction(UIMutableUserNotificationAction): 定義具體的操作(按鈕)。
UIUserNotificationCategory(UIMutableUserNotificationCategory):包含一組UIUserNotificationAction
UIUserNotificationSettings:Notification的配置信息
具體代碼如下:
//定義第一個action(按鈕) UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];//第一個按鈕 action1.identifier = @"ACTION_1";//按鈕的標(biāo)識 action1.title=@"Accept";//按鈕的標(biāo)題 action1.activationMode = UIUserNotificationActivationModeForeground;//當(dāng)點(diǎn)擊的時候啟動程序 action1.authenticationRequired = NO;//不需要認(rèn)證(解鎖) action1.destructive = NO; //定義第二個action(按鈕) UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕 action2.identifier = @"ACTION_2"; action2.title=@"Reject"; action2.activationMode = UIUserNotificationActivationModeBackground;//當(dāng)點(diǎn)擊的時候不啟動程序,在后臺處理 action2.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略; action2.destructive = YES; //定義一個category UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init]; category.identifier = @"LEI";//這組動作的唯一標(biāo)示 [category setActions:@[action1,action2] forContext:UIUserNotificationActionContextDefault]; //定義settings UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithObjects:categorys, nil]]; //注冊settings [[UIApplication sharedApplication] registerUserNotificationSettings:uns]; 關(guān)鍵一步: //要把本地通知的category屬性設(shè)置為和之前定義的category一樣,否則不會顯示按鈕 localNotification.category = @"LEI"; 最后: [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; ......
之后再appDelegate中會有對應(yīng)的事件處理函數(shù):
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ }
注:appDelegate中有眾多的事件處理函數(shù),例如:
-(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
這些事件處理函數(shù)可用來處理推送通知(本地推送和遠(yuǎn)程推送)。
以上就是我對本地推送學(xué)習(xí)的所有總結(jié),希望和大家一起學(xué)習(xí)進(jìn)步。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。