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工程即可获得

发送时间设置:

 

 

原文地址:https://www.cnblogs.com/StevenHuSir/p/iOS_JPush.html