由于配置證書和注冊極光應用配置網(wǎng)上已經(jīng)很多了所以在這里就不在多說了!!!
1.首先我們要先將從極光官網(wǎng)上下載來的最新的sdk中的lib文件夾導入工程中;
順便添加一些依賴庫如下:
/**添加依賴庫
CFNetwork.framework
CoreFoundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Foundation.framework
UIKit.framework
Security.framework
libz.tbd (Xcode7以下版本是libz.dylib)
UserNotifications.framework (Xcode8及以上)
libresolv.tbd
*/
記得在工程中打開如下選項:

這些度干完之后可以先編譯一下代碼看是否會出現(xiàn)錯誤,然后我們就開始實現(xiàn)極光推送代碼了.
2.代碼實現(xiàn)
我們可以把AppDelegate 這個類加一個分類,這樣看起來方便簡潔,而且下一次使用的時候還可以重復使用.
下面是擴展后的代碼如下:
下面這一段代碼是 .h文件的內(nèi)容,讀者可以將他們復制到你新建的擴展類就好
#import"AppDelegate.h"
//引入JPush功能所需頭文件
#import"JPUSHService.h"
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import
#endif
//如果需要使用idfa功能所需要引入的頭文件(可選)
#import
@interfaceAppDelegate (Jpush)
/**
初始化Jpush
@param launchOptions launchOptions
*/
- (void)initJpush:(NSDictionary*)launchOptions;
/**
清除badge值
*/
- (void)cleanBadge;
@end
---------------------------------以下為.m文件內(nèi)容---------------------------------------
#import"AppDelegate+Jpush.h"
#define app_Key @"0c6b42b1864207bd22d1a752"
#define channel_y @"Publish channel"
#define is_Production FALSE
@implementationAppDelegate (Jpush)
- (void)initJpush:(NSDictionary*)launchOptions{
//Required
//notice: 3.0.0及以后版本注冊可以這樣寫,也可以繼續(xù)用之前的注冊方式
JPUSHRegisterEntity* entity = [[JPUSHRegisterEntityalloc]init];
entity.types=JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert;
if([[UIDevicecurrentDevice].systemVersionfloatValue] >=8.0) {
}
[JPUSHServiceregisterForRemoteNotificationConfig:entitydelegate:self];
// Optional
//獲取IDFA
//如需使用IDFA功能請?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對應值
NSString*advertisingId = [[[ASIdentifierManagersharedManager]advertisingIdentifier]UUIDString];
[JPUSHServicesetupWithOption:launchOptionsappKey:JPushKey
channel:channel_y
apsForProduction:is_Production
advertisingIdentifier:advertisingId];
//添加下面這個觀察者是為了收取極光推送自定義消息的,這個通知的添加位置是隨意的,即在你想要處理接收到自定義的消息的任何地方度可以調(diào)用,如果在除了本類外調(diào)用的話可能需要導入#import"JPUSHService.h"的頭文件.并且實現(xiàn)以下networkDidReceiveMessage:方法就行了?
//kJPFNetworkDidReceiveMessageNotification ?他是接收自定義消息通知的name
//[NotificationCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];
}
/**
清除badge值
*/
- (void)cleanBadge{
[[UIApplicationsharedApplication]setApplicationIconBadgeNumber:0];
[JPUSHServicesetBadge:0];
}
/**
注冊DeviceToken
*/
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
/// Required -注冊DeviceToken
[JPUSHServiceregisterDeviceToken:deviceToken];
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
// Required,For systems with less than or equal to iOS6
[JPUSHServicehandleRemoteNotification:userInfo];
}
#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support應用處于前臺的狀態(tài)下收到推送消息會調(diào)用此方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(NSInteger))completionHandler {
// Required
NSDictionary* userInfo = notification.request.content.userInfo;
if([notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
[JPUSHServicehandleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert);//需要執(zhí)行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置
NSLog(@"t三種類型可以選擇設置%@",userInfo);
}
// iOS 10 Support當收到推送通知后打開推送消息會調(diào)用此方法
- (void)jpushNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler {
// Required
NSDictionary* userInfo = response.notification.request.content.userInfo;
if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTriggerclass]]) {
[JPUSHServicehandleRemoteNotification:userInfo];
}
completionHandler();//系統(tǒng)要求執(zhí)行這個方法
//取得APNs標準信息內(nèi)容
NSDictionary*aps = [userInfovalueForKey:@"aps"];
NSString*content = [apsvalueForKey:@"alert"];//推送顯示的內(nèi)容
NSLog(@"系統(tǒng)要求執(zhí)行這個方法%@",content);
}
//iOS7及以上系統(tǒng),收到通知 ? ? ?此方法在接收到靜默推送的時候會調(diào)用
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHServicehandleRemoteNotification:userInfo];
//取得APNs標準信息內(nèi)容
NSDictionary*aps = [userInfovalueForKey:@"aps"];
NSString*content = [apsvalueForKey:@"alert"];//推送顯示的內(nèi)容
NSLog(@"系統(tǒng)要求執(zhí)行這個方法%@",content);
completionHandler(UIBackgroundFetchResultNewData);
//[self tongzhi];
}
以上為全部實現(xiàn)代碼 ,在使用的時候只需要在相應位置調(diào)用方法就行了,調(diào)用代碼如下圖:


就這樣你就可以順利的接收到極光推送的消息了!!!!
3.如果要接受靜默推送的話還需要在工程中打開如下選項:

此文僅為個人見解,如我更好的建議可以隨時聯(lián)系,謝謝大家的閱讀!!!!!!!!!!