IOS集成JPush

本篇文章采用Xcode手动集成JPush

证书

参考网址:https://docs.jiguang.cn//jpush/client/iOS/ios_cer_guide/

下载SDK

下载网址:https://docs.jiguang.cn//jpush/resources/
image.png

  • SDK目录image.png
导入SDK
  • 将Lib中的.h文件与.a文件导入项目image.png
  • .h文件image.png
  • .a文件image.png
手动导入
  • 添加框架
    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)
构建设置

如果你的工程需要支持小于7.0的iOS系统,请到构建设置关闭bitCode选项,否则将无法正常编译通过。
设置搜索路径下的用户头搜索路径和库搜索路径,比如SDK文件夹(默认为lib)与工程文件在同一级目录下,则都设置为“$(SRCROOT)/ {静态库所在文件夹名称} “即可。

功能

如使用Xcode 8及以上环境开发,请开启Application Target的功能 - >推送通知选项,如图: image.png

允许Xcode 7支持Http传输方法
  • 在项目的info.plist中添加一个Key:NSAppTransportSecurity,类型为字典类型。
  • 然后给它添加一个NSExceptionDomains,类型为字典类型;
  • 把需要的支持的域添加给NSExceptionDomains。其中jpush.cn作为Key,类型为字典类型。
  • 每个域下面需要设置2个属性:NSIncludesSubdomains,NSExceptionAllowsInsecureHTTPLoads。两个属性均为布尔类型,值分别为YES,YES。image.png

代码配置

添加头文件
  • 将以下代码添加到AppDelegate.m引用头文件的位置。
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  • 添加代表
@interface AppDelegate ()<JPUSHRegisterDelegate>

@end
添加初始化APNs代码
  • 请将以下代码添加到 - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  //Required
  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
  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];
添加初始化JPush代码
  • 将以下代码添加到 - (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  // Required
  // init Push
  // notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
  // 如需继续使用 pushConfig.plist 文件声明 appKey 等配置内容,请依旧使用 [JPUSHService setupWithOption:launchOptions] 方式初始化。
  [JPUSHService setupWithOption:launchOptions appKey:@"你的appkey"
                        channel:@"Publish channel"
               apsForProduction:NO
          advertisingIdentifier:nil];
  • 部分参数说明:
  • APPKEY
    • 选择Web Portal上的应用,点击“设置”获取其appkey值。请确保应用内配置的appkey与Portal上创建应用后生成的appkey一致。
  • 渠道
    • 指明应用程序包的下载渠道,为方便分渠道统计,具体值由你自行定义,如:App Store。
  • apsForProduction
    • 1.3.1版本新增,用于标识当前应用所使用的APNs证书环境。
    • 0(默认值)表示采用的是开发证书,1表示采用生产证书发布应用。
    • 注:此字段的值要与Build Settings的Code Signing配置的证书环境一致。
  • advertisingIdentifier
注册APNs成功并上报DeviceToken
  • 在AppDelegate.m实现该回调方法并添加回调方法中的代码
- (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);
}
添加处理APNs通知回调方法
  • 在AppDelegate.m实现该回调方法并添加回调方法中的代码
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
  if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //从通知界面直接进入应用
  }else{
    //从通知设置界面进入应用
  }
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
  // 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 (^)())completionHandler {
  // 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];
}
成功运行
  • 真机调试该项目,如果控制台输出以下日志则代表您已经集成成功。
2016-08-19 17:12:12.745823 219b28[1443:286814]  | JPUSH | I - [JPUSHLogin]
----- login result -----
uid:5460310207
registrationID:171976fa8a8620a14a4
消息推送

参考上一篇文章(JAVA集成JPush):https://blog.csdn.net/weixin_41182727/article/details/98934176

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maggieq8324

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值