個(gè)推我遇到的坑,

iOS10,XCode8.1以來(lái) ? 推送據(jù)說(shuō)各種變化 ?所以自然得適配。一拖再拖,今天和個(gè)推做個(gè)了斷。之前用過(guò)極光 ?友盟。首次接觸個(gè)推

,發(fā)現(xiàn)有區(qū)別。個(gè)推不能同時(shí)開(kāi)發(fā)和生產(chǎn)環(huán)境共存,而且重要的是,環(huán)境證書(shū)切換得等十幾分鐘才生效,這一點(diǎn)得注意。個(gè)推有透?jìng)飨?,而且根?jù)cid.好

長(zhǎng)話(huà)短說(shuō) ?進(jìn)入正題。

鏈接文檔 ?SDK ?應(yīng)該都是信手拈來(lái)

http://docs.getui.com/mobile/ios/overview/

111

2222

3333

文件夾下有兩個(gè).h兩個(gè).a文件 ? 最后才發(fā)現(xiàn)我這邊只用到中間兩個(gè) ?第一個(gè)和第四個(gè)應(yīng)該是新系統(tǒng)的特性,也就是官方文檔所說(shuō)的 ?個(gè)推高級(jí)功能 ?暫且不說(shuō) ?先把推送功能打通。配置完成,接下來(lái)是一系列的復(fù)制粘貼,如文檔所示

---------------初始化SDK并注冊(cè)APNs // AppDelegate.h#import#import "GeTuiSdk.h" ? ? // GetuiSdk頭文件應(yīng)用// iOS10 及以上需導(dǎo)入 UserNotifications.framework#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0#import#endif/// 使用個(gè)推回調(diào)時(shí),需要添加"GeTuiSdkDelegate"http:/// iOS 10 及以上環(huán)境,需要添加 UNUserNotificationCenterDelegate 協(xié)議,才能使用 UserNotifications.framework 的回調(diào)@interface AppDelegate : UIResponder/// 個(gè)推開(kāi)發(fā)者網(wǎng)站中申請(qǐng)App時(shí),注冊(cè)的AppId、AppKey、AppSecret

#define kGtAppId ? ? ? ? ? @"??? "

#define kGtAppKey ? ? ? ? ?@" "

#define kGtAppSecret ? ? ? @"?? "

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// 通過(guò)個(gè)推平臺(tái)分配的appId、 appKey 、appSecret 啟動(dòng)SDK,注:該方法需要在主線(xiàn)程中調(diào)用

[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];

// 注冊(cè) APNs

[self registerRemoteNotification];

return YES;

}

/** 注冊(cè) APNs */

- (void)registerRemoteNotification {

/*

警告:Xcode8 需要手動(dòng)開(kāi)啟"TARGETS -> Capabilities -> Push Notifications"

*/

/*

警告:該方法需要開(kāi)發(fā)者自定義,以下代碼根據(jù) APP 支持的 iOS 系統(tǒng)不同,代碼可以對(duì)應(yīng)修改。

以下為演示代碼,注意根據(jù)實(shí)際需要修改,注意測(cè)試支持的 iOS 系統(tǒng)都能獲取到 DeviceToken

*/

if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8編譯會(huì)調(diào)用

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

center.delegate = self;

[center

requestAuthorizationWithOptions:(UNAuthorizationOptionBadge |

UNAuthorizationOptionSound | UNAuthorizationOptionAlert |

UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError

*_Nullable error) {

if (!error) {

NSLog(@"request authorization succeeded!");

}

}];

[[UIApplication sharedApplication] registerForRemoteNotifications];

#else // Xcode 7編譯會(huì)調(diào)用

UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerForRemoteNotifications];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

#endif

} else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerForRemoteNotifications];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

} else {

UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |

UIRemoteNotificationTypeSound |

UIRemoteNotificationTypeBadge);

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];

}

}

-----------------向個(gè)推服務(wù)器注冊(cè)DeviceToken

/** 遠(yuǎn)程通知注冊(cè)成功委托 */

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

NSString

*token = [[deviceToken description]

stringByTrimmingCharactersInSet:[NSCharacterSet

characterSetWithCharactersInString:@"<>"]];

token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token);

// 向個(gè)推服務(wù)器注冊(cè)deviceToken

[GeTuiSdk registerDeviceToken:token];

}

-------------------Background Fetch 接口回調(diào)處理

-

(void)application:(UIApplication *)application

performFetchWithCompletionHandler:(void

(^)(UIBackgroundFetchResult))completionHandler {

/// Background Fetch 恢復(fù)SDK 運(yùn)行

[GeTuiSdk resume];

completionHandler(UIBackgroundFetchResultNewData);

}

---------------統(tǒng)計(jì)APNs通知的點(diǎn)擊數(shù)

在iOS 10 以前,為處理 APNs 通知點(diǎn)擊事件,統(tǒng)計(jì)有效用戶(hù)點(diǎn)擊數(shù),需在AppDelegate.m里的didReceiveRemoteNotification回調(diào)方法中調(diào)用個(gè)推SDK統(tǒng)計(jì)接口:

-

(void)application:(UIApplication *)application

didReceiveRemoteNotification:(NSDictionary *)userInfo

fetchCompletionHandler:(void

(^)(UIBackgroundFetchResult))completionHandler {

// 將收到的APNs信息傳給個(gè)推統(tǒng)計(jì)

[GeTuiSdk handleRemoteNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);

}

對(duì)于iOS

10 及以后版本,為處理 APNs 通知點(diǎn)擊,統(tǒng)計(jì)有效用戶(hù)點(diǎn)擊數(shù),需先添加

UNUserNotificationCenterDelegate,然后在AppDelegate.m的

didReceiveNotificationResponse回調(diào)方法中調(diào)用個(gè)推SDK統(tǒng)計(jì)接口:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

// ?iOS 10: App在前臺(tái)獲取到通知

-

(void)userNotificationCenter:(UNUserNotificationCenter *)center

willPresentNotification:(UNNotification *)notification

withCompletionHandler:(void

(^)(UNNotificationPresentationOptions))completionHandler {

NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);

// 根據(jù)APP需要,判斷是否要提示用戶(hù)Badge、Sound、Alert

completionHandler(UNNotificationPresentationOptionBadge

| UNNotificationPresentationOptionSound |

UNNotificationPresentationOptionAlert);

}

// ?iOS 10: 點(diǎn)擊通知進(jìn)入App時(shí)觸發(fā),在該方法內(nèi)統(tǒng)計(jì)有效用戶(hù)點(diǎn)擊數(shù)

-

(void)userNotificationCenter:(UNUserNotificationCenter *)center

didReceiveNotificationResponse:(UNNotificationResponse *)response

withCompletionHandler:(void (^)())completionHandler {

NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);

// [ GTSdk ]:將收到的APNs信息傳給個(gè)推統(tǒng)計(jì)

[GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];

completionHandler();

}

#endif

------------------------獲取CID信息

個(gè)推SDK初始化完成后,可以在[GeTuiSdkDelegate GeTuiSdkDidRegisterClient]回調(diào)方法中獲取注冊(cè)成功的ClientID(即CID):

/** SDK啟動(dòng)成功返回cid */

- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {

//個(gè)推SDK已注冊(cè),返回clientId

NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);

}

好了 ?這會(huì)推送就應(yīng)該可以了。附加客服給我的回復(fù)。最后就是打包測(cè)試了 ?親測(cè)有效

第一次提筆 ?多多擔(dān)待 ?有問(wèn)題及時(shí)溝通!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容