為應(yīng)用創(chuàng)建 APNs 推送證書,可以按照個(gè)推說明的去配置證書和描述文件
到個(gè)推注冊(cè)應(yīng)用取得AppId,AppKey,AppSecret蜻拨,應(yīng)用注冊(cè)號(hào)之后不要立即使用,等上10分鐘后使用
開始集成抬虽,導(dǎo)入sdk及所需要的庫官觅,然后是打開推送功能
開啟 Target -> Capabilities -> Push Notifications纵菌。如果沒有開啟阐污,在 Xcode8 上編譯中將獲取不到 DeviceToken。
SDK后臺(tái)運(yùn)行權(quán)限設(shè)置
在AppDelegate 中注冊(cè) GeTuiSdkDelegate
// iOS10 及以上需導(dǎo)入 UserNotifications.framework
#import<UserNotifications/UserNotifications.h>
/// 個(gè)推開發(fā)者網(wǎng)站中申請(qǐng)App時(shí)咱圆,注冊(cè)的AppId笛辟、AppKey、AppSecret
#define kGtAppId? ? ? ? ? @"iMahVVxurw6BNr7XSn9EF2" ?
#define kGtAppKey? ? ? ? ? @"yIPfqwq6OMAPp6dkqgLpG5"
#define kGtAppSecret? ? ? @"G0aBqAD6t79JfzTB6Z5lo5"
/// 需要使用個(gè)推回調(diào)時(shí)序苏,需要添加"GeTuiSdkDelegate" /// iOS 10 及以上手幢,需要添加 UNUserNotificationCenterDelegate 協(xié)議,才能使用 UserNotifications.framework 的回調(diào) @interface AppDelegate : UIResponder<UIApplicationDelegate, GeTuiSdkDelegate, UNUserNotificationCenterDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 通過個(gè)推平臺(tái)分配的appId忱详、 appKey 围来、appSecret 啟動(dòng)SDK,注:該方法需要在主線程中調(diào)用
[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
// 注冊(cè) APNs
[self registerRemoteNotification];
return YES;
}
/** 注冊(cè) APNs */
- (void)registerRemoteNotification {
/*
警告:Xcode8 的需要手動(dòng)開啟“TARGETS -> Capabilities -> Push Notifications”
*/
/*
警告:該方法需要開發(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];
}
GeTuiSdk注冊(cè)回調(diào)院刁,獲取CID信息
/** SDK啟動(dòng)成功返回cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
//個(gè)推SDK已注冊(cè),返回clientId
NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
}
/** SDK遇到錯(cuò)誤回調(diào) */
- (void)GeTuiSdkDidOccurError:(NSError *)error {
//個(gè)推錯(cuò)誤報(bào)告粪狼,集成步驟發(fā)生的任何錯(cuò)誤都在這里通知退腥,如果集成后,無法正常收到消息再榄,查看這里的通知狡刘。
NSLog(@"\n>>>[GexinSdk error]:%@\n\n", [error localizedDescription]);
}
iOS 10 中,處理 APNs 展示點(diǎn)擊不跟,統(tǒng)計(jì)有效用戶點(diǎn)擊數(shù)颓帝,需先添加 UNUserNotificationCenterDelegate 后,在 AppDelegate.m 實(shí)現(xiàn)的 didReceiveNotificationResponse 方法中調(diào)用處理方法:
//? iOS 10: App在前臺(tái)獲取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"willPresentNotification:%@", notification.request.content.userInfo);
// 根據(jù)APP需要窝革,判斷是否要提示用戶Badge购城、Sound、Alert
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
//? iOS 10: 點(diǎn)擊通知進(jìn)入App時(shí)觸發(fā)虐译,在該方法內(nèi)統(tǒng)計(jì)有效用戶點(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();
}
設(shè)置別名瘪板,別名推送
// 綁定別名
[GeTuiSdk bindAlias:@"個(gè)推" andSequenceNum:@"seq-1"];
// 取消綁定別名
[GeTuiSdk unbindAlias:@"個(gè)推" andSequenceNum:@"seq-2"];
處理 綁定/解綁 返回:
- (void)GeTuiSdkDidAliasAction:(NSString *)action result:(BOOL)isSuccess sequenceNum:(NSString *)aSn error:(NSError *)aError {
// ?解綁別名必須在賬號(hào)退出之前解綁
if ([kGtResponseBindType isEqualToString:action]) {
NSLog(@"綁定結(jié)果 :%@ !, sn : %@", isSuccess ? @"成功" : @"失敗", aSn);
if (!isSuccess) {
NSLog(@"失敗原因: %@", aError);
}
} else if ([kGtResponseUnBindType isEqualToString:action]) {
NSLog(@"綁定結(jié)果 :%@ !, sn : %@", isSuccess ? @"成功" : @"失敗", aSn);
if (!isSuccess) {
NSLog(@"失敗原因: %@", aError);
}
}
}
使用個(gè)推 SDK 透?jìng)飨? 由個(gè)推通道下發(fā) (非 APNs)
// ?這個(gè)感覺就是個(gè)推比別的推送好用的地方了,只要clientID在線就可以直接聊天漆诽,類似與及時(shí)通訊
/** SDK收到透?jìng)飨⒒卣{(diào) */
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
//收到個(gè)推消息
NSString *payloadMsg = nil;
if (payloadData) {
payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes
length:payloadData.length
encoding:NSUTF8StringEncoding];
}
NSString *msg = [NSString stringWithFormat:@"taskId=%@,messageId:%@,payloadMsg:%@%@",taskId,msgId, payloadMsg,offLine ? @"<離線消息>" : @""];
NSLog(@"\n>>>[GexinSdk ReceivePayload]:%@\n\n", msg);
[GeTuiSdk sendFeedbackMessage:90001 andTaskId:taskId andMsgId:msgId];
if (offLine) { ? ?// ?離線消息已經(jīng)有蘋果的apns推過消息了侮攀,避免上線后再次受到消息
return;
}
// ?處理應(yīng)用在線收到的消息
}
蘋果官方靜默推送
/** APP已經(jīng)接收到“遠(yuǎn)程”通知(推送) - 透?jìng)魍扑拖? */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
// 處理APNs代碼,通過userInfo可以取到推送的信息(包括內(nèi)容厢拭,角標(biāo)兰英,自定義參數(shù)等)。如果需要彈窗等其他操作供鸠,則需要自行編碼畦贸。
NSLog(@"\n>>>[Receive RemoteNotification - Background Fetch]:%@\n\n",userInfo);
completionHandler(UIBackgroundFetchResultNewData);
// ?處理應(yīng)用不在線時(shí)受到的消息,用戶點(diǎn)擊推送消息后的跳轉(zhuǎn)
}