一直更新了iOS8,但是一直沒有開始研究這個iOS8,今天因爲項目用到了推送,於是體驗了iOS8的推送,先講講這個推送。目前分爲四個推送:用戶推送,本地推送,遠程推送,地理位置推送。
用戶推送
我們先開始講這個用戶推送,我們要使用之前必須先註冊這個推送,用戶要允許這個程序進行推送
註冊過程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
if (IS_IOS8) { //1.創建消息上面要添加的動作(按鈕的形式顯示出來) 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.創建動作(按鈕)的類別集合 UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init]; categorys.identifier = @ "alert" ; //這組動作的唯一標示,推送通知的時候也是根據這個來區分 [categorys setActions:@[action,action2] forContext:(UIUserNotificationActionContextMinimal)]; //3.創建UIUserNotificationSettings,並設置消息的顯示類類型 UIUserNotificationSettings *notiSettings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIRemoteNotificationTypeSound) categories:[NSSet setWithObjects:categorys, nil nil]]; [application registerUserNotificationSettings:notiSettings]; } else { [application registerForRemoteNotificationTypes: UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; } - ( void )application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { // UIUserNotificationSettings *settings = [application currentUserNotificationSettings]; // UIUserNotificationType types = [settings types]; // //只有5跟7的時候包含了 UIUserNotificationTypeBadge // if (types == 5 || types == 7) { // application.applicationIconBadgeNumber = 0; // } //註冊遠程通知 [application registerForRemoteNotifications]; } |
我們現在僅僅是註冊了通知的設置,還要註冊推送通知的行爲,在iOS8中,行爲能直接在推送消息進行,如回覆消息,拒絕消息等總結就是三個方法進行註冊
我們如何能進行這些行爲,首先我們需註冊這些行爲。
-
Actions
-
123456
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @
"RickAction"
;
acceptAction.title = @
"Accept"
;
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
acceptAction.destructive = NO;
acceptAction.authenticationRequired = NO;
-
Categories
-
123
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
inviteCategory.identifier = @
"INVITE_CATEGORY"
;
[inviteCategory setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];
-
我們需要注意這個UIUserNotificationActionContextDefault,如果我們使用這個,我們會得到這個推送行爲,Maybe和Accept
我們還可以使用UIUserNotificationActionContextMinimal得到的是Decline和Accept行爲
-
Settings
在這些行爲註冊之後,我們加上之前提到的推送設置就完成了註冊推送的這個流程了
1
2
3
4
|
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil nil]; UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert ; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; |
遠程推送
遠程推送,所有消息大小不超過2KB,我們獲取遠程推送的json格式的消息,解析這個消息就是我們的遠程推送了:
1
2
3
4
5
6
7
8
|
{ “aps”: { "content-available" : 1, "alert" : "This is the alert text" , "badge" : 1, "sound" : "default" } } |
若要使用遠程推送,滿足兩個條件:一、用戶需要調用註冊用戶推送registerUserNotificationSettings;二、在info.plist文件中UIBackgroundModes必須包含遠程通知。
1
2
3
4
5
6
7
8
9
10
11
12
|
[[UIApplication sharedApplication] registerForRemoteNotifications]; - ( void )application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token=[NSString stringWithFormat:@ "%@" ,deviceToken]; token=[token stringByReplacingOccurrencesOfString:@ "<" withString:@ "" ]; token=[token stringByReplacingOccurrencesOfString:@ ">" withString:@ "" ]; token=[token stringByReplacingOccurrencesOfString:@ " " withString:@ "" ]; } - ( void )application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { } |
iOS7通知代理方法
後來又增加了本地通知的代理方法
iOS8的推送代理方法只有兩個了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
- ( void )application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:( void (^)())completionHandler { } - ( void )application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:( void (^)())completionHandler { } - ( void )application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:( void (^)())completionHandler { if ([identifier isEqualToString:@ "RickAction" ]) { [self handleAcceptActionWithNotification:notification]; } completionHandler(); } - ( void )handleAcceptActionWithNotification:(UILocalNotification*)notification { } |
地理位置推送
這個推送是新的API纔有的特性,必須配合CLLocation定位一起使用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//Location Notification CLLocationManager *locMan = [[CLLocationManager alloc] init]; locMan.delegate = self; [locMan requestWhenInUseAuthorization]; #pragma mark - CLLocationManager - ( void )locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status { BOOL canUseLocationNotifications = (status == kCLAuthorizationStatusAuthorizedWhenInUse); if (canUseLocationNotifications) { [self startShowLocationNotification]; } } - ( void )application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { CLRegion *region = notification.region; if (region) { } } - ( void )startShowLocationNotification { CLLocationCoordinate2D local2D ; local2D.latitude = 123.0; local2D.longitude = 223.0; UILocalNotification *locNotification = [[UILocalNotification alloc] init]; locNotification.alertBody = @ "你接收到了" ; locNotification.regionTriggersOnce = YES; locNotification.region = [[CLCircularRegion alloc] initWithCenter:local2D radius:45 identifier:@ "local-identity" ]; [[UIApplication sharedApplication] scheduleLocalNotification:locNotification]; } |
如果沒有開啓Core Location 那麼上面的didReceiveLocalNotification不會被調用
最後再總結一下,整個推送流程我覺得是這樣子的,先註冊推送,然後推送消息,客戶端接收推送消息,執行推送行爲。如果有錯誤,還請在文章下面評論,歡迎指正。