iOS極光推送JPush集成和使用


極光推送JPush集成

一、JPushSDK集成指南

https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/

二、創建應用

在 JPush 的管理 Portal 上創建應用,創建成功后自動生成 AppKey 用以標識該應用。

在推送設置 iOS 模塊上傳 APNs 證書或配置 Token Authentication。如果對 APNs 證書不太了解 請參考

證書設置指南:https://docs.jiguang.cn/jpush/client/iOS/ios_cer_guide/

 

三、導入最新SDK

https://docs.jiguang.cn/jpush/resources/

選擇 1:Cocoapods 導入

pod 'JPush'

注:如果無法導入最新版本,請執行 pod repo update master 這個命令來升級本機的 pod 庫,然后重新 pod 'JPush'

如果需要安裝指定版本則使用以下方式(以 3.1.0 版本為例):

pod 'JPush', '3.1.0'

選擇 2:手動導入

將 SDK 包解壓,在 Xcode 中選擇 “Add files to 'Your project name'...”,將解壓后的 lib 子文件夾(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目錄中。

添加 Framework

  • CFNetwork.framework
  • CoreFoundation.framework
  • CoreTelephony.framework
  • SystemConfiguration.framework
  • CoreGraphics.framework
  • Foundation.framework
  • UIKit.framework
  • Security.framework
  • libz.tbd(Xcode 7 以下版本是 libz.dylib)
  • AdSupport.framework(獲取 IDFA 需要;如果不使用 IDFA,請不要添加)
  • UserNotifications.framework(Xcode 8 及以上)
  • libresolv.tbd(JPush 2.2.0 及以上版本需要,Xcode 7 以下版本是 libresolv.dylib)

Build Settings 關閉 bitCode 選項

請開啟 Application Target 的 Capabilities->Push Notifications 選項,如圖: 

四、實現代碼部分:

 

// 引入 JPush 功能所需頭文件
#import "JPUSHService.h"
// iOS10 注冊 APNs 所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

//JPush配置
static BOOL isProduction = FALSE;//0(默認值)表示采用的是開發證書,1 表示采用生產證書發布應用。
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
@property (strong, nonatomic) UIWindow *window;

@end
//9.JPush初始化
    [self jPushConfig];
    //NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //如果沒有使用 IDFA 直接傳 nil
    [JPUSHService setupWithOption:launchOptions appKey:JPUSH_APPKEY
                          channel:@"App Store"
                 apsForProduction:isProduction
            advertisingIdentifier:nil];

//注冊 APNs 成功並上報 DeviceToken
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    /// Required - 注冊 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}
//實現注冊 APNs 失敗接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
jPushConfig實現代碼:
@interface AppDelegate()<WXApiDelegate,JPUSHRegisterDelegate>
@end
@implementation AppDelegate (Config)

///JPush初始化
- (void)jPushConfig {
    //1.添加初始化 APNs 代碼
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        // 可以添加自定義 categories
        // NSSet<UNNotificationCategory *> *categories for iOS10 or later
        // NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];

}
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];
    NSString *content = [userInfo valueForKey:@"content"];
    NSString *messageID = [userInfo valueForKey:@"_j_msgid"];
    NSDictionary *extras = [userInfo valueForKey:@"extras"];
    NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的 Extras 附加字段,key 是自己定義的
    NSLog(@"content=%@messageID=%@extras=%@customizeField1=%@",content,messageID,extras,customizeField1);
}

//添加處理 APNs 通知回調方法
#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler  API_AVAILABLE(ios(10.0)){
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要執行這個方法,選擇是否提醒用戶,有 Badge、Sound、Alert 三種類型可以選擇設置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系統要求執行這個方法
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required, For systems with less than or equal to iOS 6
    [JPUSHService handleRemoteNotification:userInfo];
}

五、JPush后台配置:

點擊應用設置-》推送設置-》上傳生產正式和發布證書-》選擇鑒權方式-證書

是否將生產證書用於開發環境是 :此開關可配置是否是開發證書

 

導出步驟參考證書配置:

證書設置指南:https://docs.jiguang.cn/jpush/client/iOS/ios_cer_guide/

六、發送推送消息

點擊推送-》發送通知

配置證推送內容、目標平台、目標人群、發送時間

 

目標人群可以單獨設置:Registration ID 運行Xcode工程即可獲得

發送時間設置:

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM