unity接入極光推送(iOS篇)
環境:unity5.2 + eclipse + xcode8;安卓打包模式:eclipse出jar包,unity一鍵打包
提示:安卓無法做到殺掉程序後也接收推送,除非你跟手機系統運營商合作(已經和極光商務確認過),iOS是沒問題的。
(開始之前先在極光後臺建立App,流程簡單,這裡不多說)
iOS接入:(Android接入傳送門)
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中開啟推送許可權
注意:xcode9以下的版本會報找不到IOSurface.FrameWork的錯誤,可以在網上下載一個,然後儲存到此目錄
Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks
7.在專案的info.plist中新增一個Key
按下圖修改:
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接入就完成了,直接執行會提示是否允許傳送通知許可權,也代表接入完成