ios极光推送

iOS 极光推送集成
分类: IOS开发笔记   2324人阅读  评论(2)  收藏  举报

稍稍研究了一下极光推送,其实是非常简单的,不过这个过程也出现了一些问题。

对于应用在前台时,需要额外处理一下。

关于极光推送,由于在iOS8之后,有了新的API,因此极光也给我们提供了适配的API。

下面我就把对极光推送相关API的封装提取出来,希望对大家有帮助,同时也当是总结。


下面是对极光推送而封装的一个工具类:

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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  

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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  

下面就是测试一个推送功能了:

[objc]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  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 

转自:http://blog.csdn.net/woaifen3344/article/details/41311749
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值