標籤:
iOS8擁有了全新的通知中樞,有全新的通知機制。當螢幕頂部收到推送時只需要往下拉,就能看到快速控制項目介面,並不需要進入該應用才能操作。在鎖定畫面,對於推送項目也可以快速處理。基本上就是讓使用者盡量在不離開當前頁面的前提下處理推送資訊,再次提高處理效率。
能夠進行直接互動的簡訊、郵件、日曆、提醒,第三方應用,可以讓你不用進入程式就能進行快捷操作,並專註於手中正在做的事情,使用者可以做如下操作:
- 在通知橫幅快速回複資訊,不用進入簡訊程式;
- 可直接拒絕或接受郵件邀請;
- 可對提醒進行標記為完成或延遲;
- 當第三方應用程式更新介面後便可直接對應用進行快速控制項目.
最常見的簡訊的快速回複,以下面的一條廣告簡訊為例說明下如何使用快速回複的功能,具體如下所示:
最近在網上找了點資料,對這個功能進行了一下簡單的實現,具體的步驟如下:
建立步驟
在AppDelegate中的didFinishLaunchingWithOptions,按照下面的步驟加入各個程式碼片段:
1.建立訊息上要添加的動作,以按鈕的形式顯示
//接受按鈕UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];acceptAction.identifier = @"acceptAction";acceptAction.title = @"接受";acceptAction.activationMode = UIUserNotificationActivationModeForeground;//拒絕按鈕 UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];rejectAction.identifier = @"rejectAction";rejectAction.title = @"拒絕";rejectAction.activationMode = UIUserNotificationActivationModeBackground;rejectAction.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;rejectAction.destructive = YES;
2.建立動作(按鈕)的類別集合
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];categorys.identifier = @"alert";NSArray *actions = @[acceptAction, rejectAction];[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];
3.建立UIUserNotificationSettings,並設定訊息的顯示類型
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
4.註冊推送
[[UIApplication sharedApplication] registerForRemoteNotifications];[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
在使用Push的時候需要在資料包中加入特定的Category欄位(欄位內容需要前後端定義為一致),終端接收到到後,就能展示上述代碼對應Category設定的按鈕,和響應按鈕事件。
{"aps":{"alert":"測試推送的快捷回複", "sound":"default", "badge": 1, "category":"alert"}}
說明:Push資料包之前能帶的資料最多為256位元組,現在APPLE將該數值放大到2KB。
按鈕處理事件
1.以本地通知為例介紹怎麼處理,如下代碼是發起本地通知事件:
UILocalNotification *notification = [[UILocalNotification alloc] init];notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];notification.timeZone = [NSTimeZone defaultTimeZone];notification.alertBody = @"測試推送的快捷回複";notification.category = @"alert";[[UIApplication sharedApplication] scheduleLocalNotification:notification];
2.實現下面的Delegate方法
(1)成功註冊registerUserNotificationSettings後回調的方法
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ NSLog(@"%@", notificationSettings);}
(2)事件處理
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{ //在非本App介面時收到本地訊息,下拉訊息會有快捷回複的按鈕,點擊按鈕後調用的方法,根據identifier來判斷點擊的哪個按鈕,notification為訊息內容 NSLog(@"%@----%@",identifier,notification); //處理完訊息,最後一定要調用這個代碼塊 completionHandler();}
運行之後要按shift + command + H,讓程式推到後台,或者按command+L讓模擬器鎖屏,才會看到效果!如果是程式退到後台了,收到訊息後下拉訊息,則會出現剛才添加的兩個按鈕;如果是鎖屏了,則出現訊息後,左劃就會出現剛才添加的兩個按鈕。
最近研究了下iOS8的官方文檔,對這項功能進行了鑽研,基本上效果已經出來。因為遠程訊息推送比較繁瑣,需要後台支援,所以我用本地推送來代替的,基本上它們要調用的代理方法類似。長話短說,下面我就說下基本的流程:
1.建立訊息上面要添加的動作(按鈕的形式顯示出來)
[objc] view plain copy
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";//按鈕的標示
- action.title=@"Accept";//按鈕的標題
- action.activationMode = UIUserNotificationActivationModeForeground;//當點擊的時候啟動程式
- // action.authenticationRequired = YES;
- // action.destructive = YES;
-
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init]; //第二按鈕
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;//當點擊的時候不啟動程式,在幕後處理
- action.authenticationRequired = YES;//需要解鎖才能處理,如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;
- action.destructive = YES;
2.建立動作(按鈕)的類別集合
[objc] view plain copy
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";//這組動作的唯一標示
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
3.建立UIUserNotificationSettings,並設定訊息的顯示類類型
[objc] view plain copy
- UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
4.註冊推送
[objc] view plain copy
-
- [[UIApplication sharedApplication] registerUserNotificationSettings:uns];
- <pre name="code" class="objc">[[UIApplication sharedApplication] registerForRemoteNotifications];
UserRequires call to registerUserNotificationSettings:? SilentInfo.plist UIBackgroundModes array contains remote-notification??Can use both
離線push資料包帶上特定Category欄位(欄位內容需要前後台一起定義,必須要保持一致),手機端收到時,就能展示上述代碼對應Category設定的按鈕,和響應按鈕事件。
// payload example: {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}
重大修改: 離線push資料包之前能帶的資料最多為256位元組,現在APPLE將該數值放大到2KB。 這個應該是只針對IOS8的。
5.發起本地推送訊息
[objc] view plain copy
- UILocalNotification *notification = [[UILocalNotification alloc] init];
- notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
- notification.timeZone=[NSTimeZone defaultTimeZone];
- notification.alertBody=@"測試推送的快捷回複";
- notification.category = @"alert";
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
-
- //用這兩個方法判斷是否註冊成功
- // NSLog(@"currentUserNotificationSettings = %@",[[UIApplication sharedApplication] currentUserNotificationSettings]);
- //[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
6.在AppDelegate.m裡面對結果進行處理
[objc] view plain copy
- //本地推播通知
- -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
- //成功註冊registerUserNotificationSettings:後,回調的方法
- NSLog(@"%@",notificationSettings);
- }
-
- -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- //收到本地推送訊息後調用的方法
- NSLog(@"%@",notification);
- }
-
- -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
- //在非本App介面時收到本地訊息,下拉訊息會有快捷回複的按鈕,點擊按鈕後調用的方法,根據identifier來判斷點擊的哪個按鈕,notification為訊息內容
- NSLog(@"%@----%@",identifier,notification);
- completionHandler();//處理完訊息,最後一定要調用這個代碼塊
- }
-
- //遠程推播通知
- -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
- //向APNS註冊成功,收到返回的deviceToken
- }
-
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
- //向APNS註冊失敗,返回錯誤資訊error
- }
-
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- //收到遠程推播通知訊息
- }
-
- -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler
- {
- //在沒有啟動本App時,收到伺服器推送訊息,下拉訊息會有快捷回複的按鈕,點擊按鈕後調用的方法,根據identifier來判斷點擊的哪個按鈕
- }
運行之後要按shift + command +H,讓程式推到後台,或者按command+L讓模擬器鎖屏,才會看到效果!
如果是程式退到後台了,收到訊息後下拉訊息,則會出現剛才添加的兩個按鈕;如果是鎖屏了,則出現訊息後,左劃就會出現剛才添加的兩個按鈕。
效果如下:
iOS 通知中樞快速回複