极光征文程序员

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

2018-12-27  本文已影响73人  Ths

iOS篇

  1. 导入框架
pod 'JPush', '3.1.0'

注意: 如果集成 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给服务器
 }];

常见收不到通知的原因

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

本文为极光征文参赛文章

上一篇 下一篇

猜你喜欢

热点阅读