iOS8擁有了全新的通知中心,有全新的通知機制。當屏幕頂部收到推送時只需要往下拉,就能看到快速操作界面,並不需要進入該應用才能操作。在鎖屏界面,對於推送項目也可以快速處理。基本上就是讓用戶儘量在不離開當前頁面的前提下處理推送信息,再次提高處理效率。
能夠進行直接互動的短信、郵件、日曆、提醒,第三方應用,可以讓你不用進入程序就能進行快捷操作,並專注於手中正在做的事情,用戶可以做如下操作:
-
在通知橫幅快速回覆信息,不用進入短信程序;
-
可直接拒絕或接受郵件邀請;
-
可對提醒進行標記爲完成或推遲;
-
當第三方應用更新接口後便可直接對應用進行快速操作.
最常見的短信的快速回復,以下面的一條廣告短信爲例說明下如何使用快速回復的功能,具體如下所示:
最近在網上找了點資料,對這個功能進行了一下簡單的實現,具體的步驟如下:
創建步驟
在AppDelegate
中的didFinishLaunchingWithOptions
,按照下面的步驟加入各個代碼片段:
1.創建消息上要添加的動作,以按鈕的形式顯示
1
2
3
4
5
6
7
8
9
10
11
12
|
//接受按鈕
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.創建動作(按鈕)的類別集合
1
2
3
4
|
UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
categorys.identifier = @"alert";
NSArray *actions = @[acceptAction, rejectAction];
[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal];
|
3.創建UIUserNotificationSettings,並設置消息的顯示類型
1
|
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil]];
|
4.註冊推送
1
2
|
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
|
在使用Push的時候需要在數據包中加入特定的Category字段(字段內容需要前後端定義爲一致),終端接收到到後,就能展示上述代碼對應Category設置的按鈕,和響應按鈕事件。
1
|
{"aps":{"alert":"測試推送的快捷回覆", "sound":"default", "badge": 1, "category":"alert"}}
|
說明:Push數據包之前能帶的數據最多爲256字節,現在APPLE將該數值放大到2KB。
按鈕處理事件
1.以本地通知爲例介紹怎麼處理,如下代碼是發起本地通知事件:
1
2
3
4
5
6
|
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後回調的方法
1
2
3
4
|
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
NSLog(@"%@", notificationSettings);
}
|
(2)事件處理
1
2
3
4
5
6
7
|
-(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
讓模擬器鎖屏,纔會看到效果!如果是程序退到後臺了,收到消息後下拉消息,則會出現剛纔添加的兩個按鈕;如果是鎖屏了,則出現消息後,左劃就會出現剛纔添加的兩個按鈕。