美文网首页极光征文程序员
极光征文 | iOS集成极光推送步骤

极光征文 | iOS集成极光推送步骤

作者: Ths | 来源:发表于2018-12-27 15:41 被阅读73次

iOS篇

  1. 导入框架
  • Cocoapods 导入
pod 'JPush', '3.1.0'
  • 手动导入(比较麻烦,不是很推荐)
    1. 在极光官网下载最新 SDK
    2. 将 SDK 包解压,在 Xcode 中选择 “Add files to 'Your project name'...”,将解压后的 lib 子文件夹(包含 JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a )添加到你的工程目录中。
      3.添加 Framework
      • CFNetwork.framework
      • CoreFoundation.framework
      • CoreTelephony.framework
      • SystemConfiguration.framework
      • CoreGraphics.framework
      • Foundation.framework
      • UIKit.framework
      • Security.framework
      • libz.tbd
      • UserNotifications.framework
      • libresolv.tbd

注意: 如果集成 JPush 3.0.1 及以上版本, 且同时集成极光其他 SDK(如:JMessage 3.0.0 及以上版本)
1. Cocoapods 导入,建议都更新为线上最新版本,来避免 JCore 版本不一致导致的冲突。
2. 手动导入,在工程中只需保留一个最新版本的 jcore-ios-x.x.x.a 静态库文件。

  1. 开启Target 的 Capabilities->Push Notifications 选项,如图:


    1.jpg
  2. 在AppDelegate.m里引入
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
  1. 初始化
    请将以下代码添加到didFinishLaunchingWithOptions:(NSDictionary *)launchOptions里
    JpushAppKey是你申请的key
    这边是大家经常会犯错的地方,apsForProduction:NO表示采用的是开发证书,YES表示采用生产证书发布应用
  //初始化APNs 
  JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
  entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
  [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
  //初始化JPush 
  #ifdef DEBUG
  [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:NO];
  #else
  [JPUSHService setupWithOption:launchOptions appKey:JpushAppKey channel:@"App store" apsForProduction:YES];
  #endif
  1. 为 AppDelegate 添加 Delegate(JPUSHRegisterDelegate) 并添加回调方法
@interface AppDelegate ()<JPUSHRegisterDelegate>
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  // Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}
@end
  1. 添加处理 APNs 通知回调方法
#pragma mark- JPUSHRegisterDelegate

// 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];
}
  1. 上传registrationID给服务器端(也可以设置别名,设置别名也在这里写)
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
        ZPLog(@"------>registrationID: %@",[JPUSHService registrationID]);
        //上传极光registrationID给服务器
 }];

常见收不到通知的原因

  • 环境不对,先检查客户端的方法
    初始化代码中 apsForProduction:isProduction 的值需要与客户端的证书环境保持一致
  • 后台Api推送的话再去看后台的,后台也有一个配置生产还是测试环境的属性.
  • 前后端的appkey不一致

如果你找不到原因,建议你可以加qq群445928015,极光有专业的客服工作日在线答疑.我使用过好多三方服务,第一次见不付费的非VIP用户也会被客服耐心解答的,经常遇到相同的问题客服也是非常有耐心,非常难得.

本文为极光征文参赛文章

相关文章

网友评论

    本文标题:极光征文 | iOS集成极光推送步骤

    本文链接:https://www.haomeiwen.com/subject/irbrlqtx.html