1疤孕、注冊個推平臺賬號拙毫。http://www.getui.com/cn/index.html
2著蛙、創(chuàng)建推送消息應用泌枪,填寫應用信息恼布,android填寫包名螺戳,iOS平臺分開發(fā)環(huán)境和正式環(huán)境,可以分別創(chuàng)建應用折汞,上傳相應的APNs的p12證書和密碼 或者等項目上線時替換成正式環(huán)境證書倔幼,10分鐘生效。
3爽待、項目集成GTSDK损同,配置AppID、AppSecret鸟款、AppKey膏燃。
4、在didFinishLaunchingWithOptions中開啟個推何什。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
}
然后注冊遠程推送蹄梢,順序不能反!
//進行用戶權(quán)限的申請
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionCarPlay completionHandler:^(BOOL granted, NSError * _Nullable error) {
//在block中會傳入布爾值granted富俄,表示用戶是否同意
if (granted) {
//如果用戶權(quán)限申請成功禁炒,設置通知中心的代理
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
}else{
UIApplication *app = [UIApplication sharedApplication];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
if ([app respondsToSelector:@selector(registerUserNotificationSettings:)]) {
NSLog(@"8.0注冊通知");
[app registerUserNotificationSettings:settings];
} else {
NSLog(@"7.0及以下 注冊通知");
[app registerForRemoteNotificationTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound];
}
}
5、在注冊成功的方法中處理一下蘋果返回的deviceToken,去掉兩端的尖括號和中間的空格
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
// 向個推服務器注冊deviceToken
[GeTuiSdk registerDeviceToken:token];
}
6霍比、注冊clientId 綁定別名功能:后臺可以根據(jù)別名進行推送
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
[GeTuiSdk bindAlias:alias andSequenceNum:clientId];
}
7幕袱、收到遠程通知消息
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// iOS 10: App在前臺獲取到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
MyLog(@"willPresentNotification:%@", notification.request.content.userInfo);
// 根據(jù)APP需要,判斷是否要提示用戶Badge悠瞬、Sound们豌、Alert
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
// iOS 10: 點擊通知進入App時觸發(fā)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
MyLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
// [ GTSdk ]:將收到的APNs信息傳給個推統(tǒng)計
[GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#endif
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// 將收到的APNs信息傳給個推統(tǒng)計
[GeTuiSdk handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
8涯捻、收到透傳消息,個推iOS推送望迎,若app在前臺運行障癌,消息走透傳,若在后臺運行辩尊,消息走通知涛浙,development環(huán)境測試推送,只能走透傳消息摄欲,通知消息僅支持Android轿亮。且收到透傳消息,沒有通知欄提示胸墙。
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
NSDictionary *dict = nil;
if (payloadData) {
dict = [NSJSONSerialization JSONObjectWithData:payloadData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"************接收到透傳消息*************************%@",dict);
}
}
最后若是收不到消息我注,問題排查:
(1)檢查一下AppId 、AppKey迟隅、AppSecret是否配置正確
(2)檢查一下deviceToken是否與上傳證書的環(huán)境一致但骨。
(3)檢查一下開啟個推和注冊遠程通知的順序
(4)注意測試的時候是沒有通知欄的,可以在接收透傳消息的代理中檢測是否接收到推送消息智袭。
======================================================
我是有底線的======================================================