99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

本地推送UILocalNotification的實現-創新互聯

最近看唐巧的技術博客,有一篇博文提到如何改進客戶端升級提醒功能(原文在這里: 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

這些事件處理函數可用來處理推送通知(本地推送和遠程推送)。

        以上就是我對本地推送學習的所有總結,希望和大家一起學習進步。

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前名稱:本地推送UILocalNotification的實現-創新互聯
本文URL:http://www.yijiale78.com/article26/pgcjg.html

成都網站建設公司_創新互聯,為您提供網站內鏈網站制作品牌網站制作商城網站Google微信小程序

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都seo排名網站優化