iOS 極光推送接受通知和自定義消息及靜默推送

由于配置證書和注冊極光應用配置網(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)用代碼如下圖:


此圖為初始化極光調(diào)用方法


此圖為顯示和取消接受通知的角標

就這樣你就可以順利的接收到極光推送的消息了!!!!

3.如果要接受靜默推送的話還需要在工程中打開如下選項:


配置選項

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

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 此處我用的是jpush-react-native,這個是極光官網(wǎng)維護的,還有一個是react-antive-jpu...
    liu_520閱讀 24,504評論 95 39
  • 不同版本極光推送SDK集成各有差異,集成時一定要注意版本號,樓主已將博文更新成最新的SDK JPush v3.0....
    i順頌時宜閱讀 8,013評論 37 170
  • 極光推送: 1.JPush當前版本是1.8.2,其SDK的開發(fā)除了正常的功能完善和擴展外也緊隨蘋果官方的步伐,SD...
    Isspace閱讀 6,873評論 10 16
  • 概述 在多數(shù)移動應用中任何時候都只能有一個應用程序處于活躍狀態(tài),如果其他應用此刻發(fā)生了一些用戶感興趣的那么通過通知...
    莫離_焱閱讀 6,708評論 1 8
  • 何年憂思何年夢,幾度風雨幾度休。 月蔽星隱云靉靆,花逝塵囂霧涳濛。 提筆可嘆腕乏力,燃燭猶殤淚盈痕。 秋窗風雨霓虹...
    文抒苑閱讀 465評論 2 4

友情鏈接更多精彩內(nèi)容