iOS8推送消息的快速回復處理

http://blog.csdn.net/yujianxiang666/article/details/35260135ios

 

 

     iOS8擁有了全新的通知中心,有全新的通知機制。當屏幕頂部收到推送時只須要往下拉,就能看到快速操做界面,並不須要進入該應用才能操做。在鎖屏界面,對於推送項目也能夠快速處理。基本上就是讓用戶儘可能在不離開當前頁面的前提下處理推送信息,再次提升處理效率。git

     可以進行直接互動的短信、郵件、日曆、提醒,第三方應用,可讓你不用進入程序就能進行快捷操做,並專一於手中正在作的事情。github

  •  在通知橫幅快速回覆信息,不用進入短信程序;
  •  可直接拒絕或接受郵件邀請;
  •  可對提醒進行標記爲完成或推遲;
  •  當第三方應用更新接口後即可直接對應用進行快速操做。

                         消息回覆                 消息快捷回覆

     最近研究了下iOS8的官方文檔,對這項功能進行了鑽研,基本上效果已經出來。由於遠程消息推送比較繁瑣,須要後臺支持,因此我用本地推送來代替的,基本上它們要調用的代理方法相似。長話短說,下面我就說下基本的流程:xcode

1.建立消息上面要添加的動做(按鈕的形式顯示出來)   服務器

 

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];  
  2.    action.identifier = @"action";//按鈕的標示  
  3.    action.title=@"Accept";//按鈕的標題  
  4.    action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啓動程序  
  5.    //    action.authenticationRequired = YES;  
  6.    //    action.destructive = YES;  
  7.   
  8.    UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按鈕  
  9.    action2.identifier = @"action2";  
  10.    action2.title=@"Reject";  
  11.    action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啓動程序,在後臺處理  
  12.    action.authenticationRequired = YES;//須要解鎖才能處理,若是action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;  
  13.    action.destructive = YES;  

 

2.建立動做(按鈕)的類別集合app

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];  
  2.     categorys.identifier = @"alert";//這組動做的惟一標示  
  3.     [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];  


3.建立UIUserNotificationSettings,並設置消息的顯示類類型ide

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];  

 

4.註冊推送測試

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1.     
  2. [[UIApplication sharedApplication] registerUserNotificationSettings:uns];  
  3. <pre name="code" class="objc">[[UIApplication sharedApplication] registerForRemoteNotifications];  

 

 

 

UserRequires call to registerUserNotificationSettings:• SilentInfo.plist UIBackgroundModes array contains remote-notification•
Can use bothui

 

離線push數據包帶上特定Category字段(字段內容須要先後臺一塊兒定義,必需要保持一致),手機端收到時,就能展現上述代碼對應Category設置的按鈕,和響應按鈕事件。spa

// payload example:  {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}

重大修改: 離線push數據包以前能帶的數據最多爲256字節,如今APPLE將該數值放大到2KB。 這個應該是隻針對IOS8的。

 

5.發起本地推送消息

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. UILocalNotification *notification = [[UILocalNotification alloc] init];  
  2.    notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];  
  3.    notification.timeZone=[NSTimeZone defaultTimeZone];  
  4.    notification.alertBody=@"測試推送的快捷回覆";  
  5.    notification.category = @"alert";  
  6.    [[UIApplication sharedApplication]  scheduleLocalNotification:notification];  
  7.      
  8.    //用這兩個方法判斷是否註冊成功  
  9.    // NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);  
  10.    //[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];  

 

6.在AppDelegate.m裏面對結果進行處理

[objc]  view plain  copy
 
 在CODE上查看代碼片派生到個人代碼片
  1. //本地推送通知  
  2. -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings  
  3. {  
  4.     //成功註冊registerUserNotificationSettings:後,回調的方法  
  5.     NSLog(@"%@",notificationSettings);  
  6. }  
  7.   
  8. -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification  
  9. {  
  10.     //收到本地推送消息後調用的方法  
  11.     NSLog(@"%@",notification);  
  12. }  
  13.   
  14. -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler  
  15. {  
  16.     //在非本App界面時收到本地消息,下拉消息會有快捷回覆的按鈕,點擊按鈕後調用的方法,根據identifier來判斷點擊的哪一個按鈕,notification爲消息內容  
  17.     NSLog(@"%@----%@",identifier,notification);  
  18.     completionHandler();//處理完消息,最後必定要調用這個代碼塊  
  19. }  
  20.   
  21. //遠程推送通知  
  22. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  23. {  
  24.     //向APNS註冊成功,收到返回的deviceToken  
  25. }  
  26.   
  27. -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  
  28. {  
  29.     //向APNS註冊失敗,返回錯誤信息error  
  30. }  
  31.   
  32. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
  33. {  
  34.     //收到遠程推送通知消息  
  35. }  
  36.   
  37. -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler  
  38. {  
  39.     //在沒有啓動本App時,收到服務器推送消息,下拉消息會有快捷回覆的按鈕,點擊按鈕後調用的方法,根據identifier來判斷點擊的哪一個按鈕  
  40. }  


運行以後要按shift + command +H,讓程序推到後臺,或者按command+L讓模擬器鎖屏,纔會看到效果!

若是是程序退到後臺了,收到消息後下拉消息,則會出現剛纔添加的兩個按鈕;若是是鎖屏了,則出現消息後,左劃就會出現剛纔添加的兩個按鈕。

效果以下:

                

如今只是能讓消息中顯示出按鈕的形式,帶輸入框的還在研究中,若是你們研究出來了,也謝謝能分享一下啊,你們一塊兒提升!

代碼:https://github.com/ios44first/PushDemo

(如要轉載請註明地址,謝謝 http://blog.csdn.net/yujianxiang666/article/details/35260135)

相關文章
相關標籤/搜索