http://blog.csdn.net/yujianxiang666/article/details/35260135ios
iOS8擁有了全新的通知中心,有全新的通知機制。當屏幕頂部收到推送時只須要往下拉,就能看到快速操做界面,並不須要進入該應用才能操做。在鎖屏界面,對於推送項目也能夠快速處理。基本上就是讓用戶儘可能在不離開當前頁面的前提下處理推送信息,再次提升處理效率。git
可以進行直接互動的短信、郵件、日曆、提醒,第三方應用,可讓你不用進入程序就能進行快捷操做,並專一於手中正在作的事情。github
- 在通知橫幅快速回覆信息,不用進入短信程序;
- 可直接拒絕或接受郵件邀請;
- 可對提醒進行標記爲完成或推遲;
- 當第三方應用更新接口後即可直接對應用進行快速操做。
![消息快捷回覆](http://static.javashuo.com/static/loading.gif)
最近研究了下iOS8的官方文檔,對這項功能進行了鑽研,基本上效果已經出來。由於遠程消息推送比較繁瑣,須要後臺支持,因此我用本地推送來代替的,基本上它們要調用的代理方法相似。長話短說,下面我就說下基本的流程:xcode
1.建立消息上面要添加的動做(按鈕的形式顯示出來) 服務器
- UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
- action.identifier = @"action";
- action.title=@"Accept";
- action.activationMode = UIUserNotificationActivationModeForeground;
-
-
-
- UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];
- action2.identifier = @"action2";
- action2.title=@"Reject";
- action2.activationMode = UIUserNotificationActivationModeBackground;
- action.authenticationRequired = YES;
- action.destructive = YES;
2.建立動做(按鈕)的類別集合app
- UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
- categorys.identifier = @"alert";
- [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)];
3.建立UIUserNotificationSettings,並設置消息的顯示類類型ide
- UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]];
4.註冊推送測試
-
- [[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 bothui
離線push數據包帶上特定Category字段(字段內容須要先後臺一塊兒定義,必需要保持一致),手機端收到時,就能展現上述代碼對應Category設置的按鈕,和響應按鈕事件。spa
// payload example: {"aps":{"alert":"Incoming call", "sound":"default", "badge": 1, "category":"incomingCall"}}
重大修改: 離線push數據包以前能帶的數據最多爲256字節,如今APPLE將該數值放大到2KB。 這個應該是隻針對IOS8的。
5.發起本地推送消息
- UILocalNotification *notification = [[UILocalNotification alloc] init];
- notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:5];
- notification.timeZone=[NSTimeZone defaultTimeZone];
- notification.alertBody=@"測試推送的快捷回覆";
- notification.category = @"alert";
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
-
-
-
-
6.在AppDelegate.m裏面對結果進行處理
- -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
- {
-
- NSLog(@"%@",notificationSettings);
- }
-
- -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
-
- NSLog(@"%@",notification);
- }
-
- -(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler
- {
-
- NSLog(@"%@----%@",identifier,notification);
- 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
- {
-
- }
運行以後要按shift + command +H,讓程序推到後臺,或者按command+L讓模擬器鎖屏,纔會看到效果!
若是是程序退到後臺了,收到消息後下拉消息,則會出現剛纔添加的兩個按鈕;若是是鎖屏了,則出現消息後,左劃就會出現剛纔添加的兩個按鈕。
效果以下:
![](http://static.javashuo.com/static/loading.gif)
如今只是能讓消息中顯示出按鈕的形式,帶輸入框的還在研究中,若是你們研究出來了,也謝謝能分享一下啊,你們一塊兒提升!
代碼:https://github.com/ios44first/PushDemo
(如要轉載請註明地址,謝謝 http://blog.csdn.net/yujianxiang666/article/details/35260135)