unity接入极光推送(iOS篇)

环境:unity5.2  + eclipse + xcode8;安卓打包模式:eclipse出jar包,unity一键打包


提示:安卓无法做到杀掉进程后也接收推送,除非你跟手机系统运营商合作(已经和极光商务确认过),iOS是没问题的。

(开始之前先在极光后台创建App,流程简单,这里不多说)


iOS接入:(Android接入传送门)

1.生成推送开发证书和发布证书
2.导入证书,右键生成.pl2文件并设置密码

3.在后台网站提交这两个文件

以上三步看这篇博客


4.导入sdk(1个头文件,2个.so)

将SDK包解压,在Xcode中选择“Add files to 'Your project name'...”,将解压后的lib子文件夹(包含JPUSHService.h、jpush-ios-x.x.x.a、jcore-ios-x.x.x.a)添加到你的工程目录中。


5.添加FrameWork

添加Framework

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


6.在xcode中开启推送权限

jpush_ios

注意:xcode9以下的版本会报找不到IOSurface.FrameWork的错误,可以在网上下载一个,然后保存到此目录

Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks


7.在项目的info.plist中添加一个Key

按下图修改:

jpush_ios


8.修改AppDelegate.m文件

(1) 添加头文件

// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>

(2)为AppDelegate添加Delegate

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

(3)请将以下代码添加到-(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions

//Required
  //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
  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];


// Optional
  // 获取IDFA
  // 如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
  NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];

  // Required
  // init Push
  // notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
  // 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
  [JPUSHService setupWithOption:launchOptions appKey:appKey
                        channel:channel
               apsForProduction:isProduction
          advertisingIdentifier:advertisingId];

JPUSHService setupWithOption可参考以下赋值:

 [JPUSHService setupWithOption:launchOptions appKey:@"xxxxxxxxxxxxxxxxx"
                        channel:@"AppStore"
               apsForProduction:1
          advertisingIdentifier:nil];

(4) 在AppDelegate.m中添加以下代码

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

  /// Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}
#pragma mark- JPUSHRegisterDelegate

// 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 iOS6
  [JPUSHService handleRemoteNotification:userInfo];
}

这两个问题要单独说一下:

1.

@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

unity导出的xcoce项目没有AppDelegate.mm,需要给UnityAppController加一层委托:

@interface UnityAppController : NSObjecs<xxxxxxxxxx,JPUSHRegisterDelegate>

2.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

  /// Required - 注册 DeviceToken
  [JPUSHService registerDeviceToken:deviceToken];
}

添加的这些周期函数,可能原来项目里面就有,有的话把函数里面内容拷贝进去就可以了


好了到这里IOS接入就完成了,直接运行会提示是否允许发送通知权限,也代表接入完成


猜你喜欢

转载自blog.csdn.net/u014261855/article/details/80270535