iOS極光推送集成

文章已移到新的博客:http://www.henishuo.com/ios-jpush/



稍稍研究了一下極光推送,其實是非常簡單的,不過這個過程也出現了一些問題。

對於應用在前臺時,需要額外處理一下。

關於極光推送,由於在iOS8之後,有了新的API,因此極光也給我們提供了適配的API。

下面我就把對極光推送相關API的封裝提取出來,希望對大家有幫助,同時也當是總結。


下面是對極光推送而封裝的一個工具類:

  1. //  
  2. //  HYBJPushHelper.h  
  3. //  JPushDemo  
  4. //  
  5. //  Created by 黃儀標 on 14/11/20.  
  6. //  Copyright (c) 2014年 黃儀標. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import <UIKit/UIKit.h>  
  11.   
  12. /*! 
  13.  * @brief 極光推送相關API封裝 
  14.  * @author huangyibiao 
  15.  */  
  16. @interface HYBJPushHelper : NSObject  
  17.   
  18. // 在應用啓動的時候調用  
  19. + (void)setupWithOptions:(NSDictionary *)launchOptions;  
  20.   
  21. // 在appdelegate註冊設備處調用  
  22. + (void)registerDeviceToken:(NSData *)deviceToken;  
  23.   
  24. // ios7以後,纔有completion,否則傳nil  
  25. + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion;  
  26.   
  27. // 顯示本地通知在最前面  
  28. + (void)showLocalNotificationAtFront:(UILocalNotification *)notification;  
  29.   
  30. @end  

  1. //  
  2. //  HYBJPushHelper.m  
  3. //  JPushDemo  
  4. //  
  5. //  Created by 黃儀標 on 14/11/20.  
  6. //  Copyright (c) 2014年 黃儀標. All rights reserved.  
  7. //  
  8.   
  9. #import "HYBJPushHelper.h"  
  10. #import "APService.h"  
  11.   
  12. @implementation HYBJPushHelper  
  13.   
  14. + (void)setupWithOptions:(NSDictionary *)launchOptions {  
  15.   // Required  
  16. #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1  
  17.   // ios8之後可以自定義category  
  18.   if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {  
  19.     // 可以添加自定義categories  
  20.     [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |  
  21.                                                    UIUserNotificationTypeSound |  
  22.                                                    UIUserNotificationTypeAlert)  
  23.                                        categories:nil];  
  24.   } else {  
  25. #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0  
  26.     // ios8之前 categories 必須爲nil  
  27.     [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |  
  28.                                                    UIRemoteNotificationTypeSound |  
  29.                                                    UIRemoteNotificationTypeAlert)  
  30.                                        categories:nil];  
  31. #endif  
  32.   }  
  33. #else  
  34.   // categories 必須爲nil  
  35.   [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |  
  36.                                                  UIRemoteNotificationTypeSound |  
  37.                                                  UIRemoteNotificationTypeAlert)  
  38.                                      categories:nil];  
  39. #endif  
  40.     
  41.   // Required  
  42.   [APService setupWithOption:launchOptions];  
  43.   return;  
  44. }  
  45.   
  46. + (void)registerDeviceToken:(NSData *)deviceToken {  
  47.   [APService registerDeviceToken:deviceToken];  
  48.   return;  
  49. }  
  50.   
  51. + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion {  
  52.   [APService handleRemoteNotification:userInfo];  
  53.     
  54.   if (completion) {  
  55.     completion(UIBackgroundFetchResultNewData);  
  56.   }  
  57.   return;  
  58. }  
  59.   
  60. + (void)showLocalNotificationAtFront:(UILocalNotification *)notification {  
  61.   [APService showLocalNotificationAtFront:notification identifierKey:nil];  
  62.   return;  
  63. }  
  64.   
  65. @end  

下面就是測試一個推送功能了:

  1. //  
  2. //  AppDelegate.m  
  3. //  JPushDemo  
  4. //  
  5. //  Created by 黃儀標 on 14/11/20.  
  6. //  Copyright (c) 2014年 黃儀標. All rights reserved.  
  7. //  
  8.   
  9. #import "AppDelegate.h"  
  10. #import "JPushHelper/HYBJPushHelper.h"  
  11.   
  12. @interface AppDelegate ()  
  13.   
  14. @end  
  15.   
  16. @implementation AppDelegate  
  17.   
  18.   
  19. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  20.   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  21.   // Override point for customization after application launch.  
  22.     
  23.   [HYBJPushHelper setupWithOptions:launchOptions];  
  24.     
  25.   self.window.backgroundColor = [UIColor whiteColor];  
  26.   [self.window makeKeyAndVisible];  
  27.   return YES;  
  28. }  
  29.   
  30. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  
  31.   [HYBJPushHelper registerDeviceToken:deviceToken];  
  32.   return;  
  33. }  
  34.   
  35. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  
  36.   [HYBJPushHelper handleRemoteNotification:userInfo completion:nil];  
  37.   return;  
  38. }  
  39.   
  40. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0  
  41. // ios7.0以後纔有此功能  
  42. - (void)application:(UIApplication *)application didReceiveRemoteNotification  
  43.                    :(NSDictionary *)userInfo fetchCompletionHandler  
  44.                    :(void (^)(UIBackgroundFetchResult))completionHandler {  
  45.   [HYBJPushHelper handleRemoteNotification:userInfo completion:completionHandler];  
  46.     
  47.   // 應用正處理前臺狀態下,不會收到推送消息,因此在此處需要額外處理一下  
  48.   if (application.applicationState == UIApplicationStateActive) {  
  49.    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"  
  50.                                                    message:userInfo[@"aps"][@"alert"]  
  51.                                                   delegate:nil  
  52.                                          cancelButtonTitle:@"取消"  
  53.                                          otherButtonTitles:@"確定", nil nil];  
  54.     [alert show];  
  55.   }  
  56.   return;  
  57. }  
  58. #endif  
  59.   
  60. - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {  
  61.   [HYBJPushHelper showLocalNotificationAtFront:notification];  
  62.   return;  
  63. }  
  64.   
  65. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {  
  66.   NSLog(@"Error in registration. Error: %@", err);  
  67. }  
  68.   
  69. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  70.   [application setApplicationIconBadgeNumber:0];  
  71.   return;  
  72. }  
  73.   
  74. @end  


真機運行,然後到官網去發一個通知,就可以收到了!

Good luck!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章