文章已移到新的博客:http://www.henishuo.com/ios-jpush/
!
稍稍研究了一下極光推送,其實是非常簡單的,不過這個過程也出現了一些問題。
對於應用在前臺時,需要額外處理一下。
關於極光推送,由於在iOS8之後,有了新的API,因此極光也給我們提供了適配的API。
下面我就把對極光推送相關API的封裝提取出來,希望對大家有幫助,同時也當是總結。
下面是對極光推送而封裝的一個工具類:
- //
- // HYBJPushHelper.h
- // JPushDemo
- //
- // Created by 黃儀標 on 14/11/20.
- // Copyright (c) 2014年 黃儀標. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- /*!
- * @brief 極光推送相關API封裝
- * @author huangyibiao
- */
- @interface HYBJPushHelper : NSObject
- // 在應用啓動的時候調用
- + (void)setupWithOptions:(NSDictionary *)launchOptions;
- // 在appdelegate註冊設備處調用
- + (void)registerDeviceToken:(NSData *)deviceToken;
- // ios7以後,纔有completion,否則傳nil
- + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion;
- // 顯示本地通知在最前面
- + (void)showLocalNotificationAtFront:(UILocalNotification *)notification;
- @end
- //
- // HYBJPushHelper.m
- // JPushDemo
- //
- // Created by 黃儀標 on 14/11/20.
- // Copyright (c) 2014年 黃儀標. All rights reserved.
- //
- #import "HYBJPushHelper.h"
- #import "APService.h"
- @implementation HYBJPushHelper
- + (void)setupWithOptions:(NSDictionary *)launchOptions {
- // Required
- #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_7_1
- // ios8之後可以自定義category
- if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
- // 可以添加自定義categories
- [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
- UIUserNotificationTypeSound |
- UIUserNotificationTypeAlert)
- categories:nil];
- } else {
- #if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
- // ios8之前 categories 必須爲nil
- [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
- UIRemoteNotificationTypeSound |
- UIRemoteNotificationTypeAlert)
- categories:nil];
- #endif
- }
- #else
- // categories 必須爲nil
- [APService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
- UIRemoteNotificationTypeSound |
- UIRemoteNotificationTypeAlert)
- categories:nil];
- #endif
- // Required
- [APService setupWithOption:launchOptions];
- return;
- }
- + (void)registerDeviceToken:(NSData *)deviceToken {
- [APService registerDeviceToken:deviceToken];
- return;
- }
- + (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion {
- [APService handleRemoteNotification:userInfo];
- if (completion) {
- completion(UIBackgroundFetchResultNewData);
- }
- return;
- }
- + (void)showLocalNotificationAtFront:(UILocalNotification *)notification {
- [APService showLocalNotificationAtFront:notification identifierKey:nil];
- return;
- }
- @end
下面就是測試一個推送功能了:
- //
- // AppDelegate.m
- // JPushDemo
- //
- // Created by 黃儀標 on 14/11/20.
- // Copyright (c) 2014年 黃儀標. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "JPushHelper/HYBJPushHelper.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- [HYBJPushHelper setupWithOptions:launchOptions];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }
- - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- [HYBJPushHelper registerDeviceToken:deviceToken];
- return;
- }
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
- [HYBJPushHelper handleRemoteNotification:userInfo completion:nil];
- return;
- }
- #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
- // ios7.0以後纔有此功能
- - (void)application:(UIApplication *)application didReceiveRemoteNotification
- :(NSDictionary *)userInfo fetchCompletionHandler
- :(void (^)(UIBackgroundFetchResult))completionHandler {
- [HYBJPushHelper handleRemoteNotification:userInfo completion:completionHandler];
- // 應用正處理前臺狀態下,不會收到推送消息,因此在此處需要額外處理一下
- if (application.applicationState == UIApplicationStateActive) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收到推送消息"
- message:userInfo[@"aps"][@"alert"]
- delegate:nil
- cancelButtonTitle:@"取消"
- otherButtonTitles:@"確定", nil nil];
- [alert show];
- }
- return;
- }
- #endif
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
- [HYBJPushHelper showLocalNotificationAtFront:notification];
- return;
- }
- - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
- NSLog(@"Error in registration. Error: %@", err);
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- [application setApplicationIconBadgeNumber:0];
- return;
- }
- @end
真機運行,然後到官網去發一個通知,就可以收到了!
Good luck!