iOS開(kāi)發(fā)個(gè)推集成中的注意點(diǎn)(手動(dòng)集成)

一、個(gè)推的集成

1.個(gè)推的集成可以參考個(gè)推的集成文檔微饥。

2.初步集成的代碼如下:

APPDelegate.h

#import <UIKit/UIKit.h>

// 個(gè)推SDK頭文件和相關(guān)信息宏(測(cè)試)
#import "GeTuiSdk.h"
#define kGtAppId     @"個(gè)推平臺(tái)登記應(yīng)用后的AppId"
#define kGtAppKey    @"個(gè)推平臺(tái)登記應(yīng)用后的AppKey"
#define kGtAppSecret @"個(gè)推平臺(tái)登記應(yīng)用后的AppSecret"

@interface AppDelegate : UIResponder <UIApplicationDelegate, GeTuiSdkDelegate>

@property (strong, nonatomic) UIWindow *window;


@end

APPDelegate.m

#import "ViewController.h"

// iOS10 及以上需導(dǎo)入 UserNotifications.framework
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
#import <UserNotifications/UserNotifications.h>
#endif


@interface AppDelegate ()<UNUserNotificationCenterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 
    // [ GTSdk ]:是否運(yùn)行電子圍欄Lbs功能和是否SDK主動(dòng)請(qǐng)求用戶定位
    [GeTuiSdk lbsLocationEnable:YES andUserVerify:YES];
    
    // [ GTSdk ]:自定義渠道
    [GeTuiSdk setChannelId:@"GT-Channel"];
    
    // [ GTSdk ]:使用APPID/APPKEY/APPSECRENT啟動(dòng)個(gè)推
    [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
    
    // 注冊(cè)APNs - custom method - 開(kāi)發(fā)者自定義的方法
    [self registerRemoteNotification];
    

    
    // 啟動(dòng)頁(yè)延時(shí)加載
    [NSThread sleepForTimeInterval:3.0];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init] ];

    self.window.rootViewController = nav;
    
    [self.window makeKeyAndVisible];

    [IQKeyboardManager sharedManager].enable = YES;

    return YES;
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark ========= 個(gè)推相關(guān)代碼 ==========
- (NSString *)formateTime:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *dateTime = [formatter stringFromDate:date];
    return dateTime;
}

#pragma mark - background fetch  喚醒
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // [ GTSdk ]:Background Fetch 恢復(fù)SDK 運(yùn)行
    [GeTuiSdk resume];
    
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark - 用戶通知(推送) _自定義方法

/** 注冊(cè)遠(yuǎn)程通知 */
- (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] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
#endif
    } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
                                                                       UIRemoteNotificationTypeSound |
                                                                       UIRemoteNotificationTypeBadge);
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
    }
}

#pragma mark - 遠(yuǎn)程通知(推送)回調(diào)

/** 遠(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);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"token" message:token delegate:nil cancelButtonTitle:@"cancle" otherButtonTitles:@"ok", nil];
    [alertView show];
    
    // [ GTSdk ]:向個(gè)推服務(wù)器注冊(cè)deviceToken
    [GeTuiSdk registerDeviceToken:token];
}

/** 遠(yuǎn)程通知注冊(cè)失敗委托 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"%@", error.localizedDescription);
}

#pragma mark - APP運(yùn)行中接收到通知(推送)處理 - iOS 10以下版本收到推送

/** APP已經(jīng)接收到“遠(yuǎn)程”通知(推送) - (App運(yùn)行在后臺(tái))  */
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler {
    
    // [ GTSdk ]:將收到的APNs信息傳給個(gè)推統(tǒng)計(jì)
    [GeTuiSdk handleRemoteNotification:userInfo];
    
    // 顯示APNs信息到頁(yè)面
    NSString *record = [NSString stringWithFormat:@"[APN]%@, %@", [NSDate date], userInfo];
    NSLog(@"%@", record);
    
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark - iOS 10中收到推送消息

#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需要沈跨,判斷是否要提示用戶Badge、Sound兔综、Alert
    completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}

//  iOS 10: 點(diǎn)擊通知進(jìn)入App時(shí)觸發(fā)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    
    NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo);
    
    [GeTuiSdk resetBadge]; //重置角標(biāo)計(jì)數(shù)
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; // APP 清空角標(biāo)
    
    // [ GTSdk ]:將收到的APNs信息傳給個(gè)推統(tǒng)計(jì)
    [GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo];
    
    completionHandler();
}
#endif


#pragma mark - GeTuiSdkDelegate

/** SDK啟動(dòng)成功返回cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
    // [ GTSdk ]:個(gè)推SDK已注冊(cè)饿凛,返回clientId
    NSLog(@">>[GTSdk RegisterClient]:%@", clientId);
}

/** SDK收到透?jìng)飨⒒卣{(diào) */
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId {
    // [ GTSdk ]:匯報(bào)個(gè)推自定義事件(反饋透?jìng)飨?
    [GeTuiSdk sendFeedbackMessage:90001 andTaskId:taskId andMsgId:msgId];
    
    // 數(shù)據(jù)轉(zhuǎn)換
    NSString *payloadMsg = nil;
    if (payloadData) {
        payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding];
    }
    
    // 頁(yè)面顯示日志
    NSString *record = [NSString stringWithFormat:@" %@, %@%@", [self formateTime:[NSDate date]], payloadMsg, offLine ? @"<離線消息>" : @""];
    NSLog(@"%@", record);
    
    // 控制臺(tái)打印日志
    NSString *msg = [NSString stringWithFormat:@"%@ : %@%@", [self formateTime:[NSDate date]], payloadMsg, offLine ? @"<離線消息>" : @""];
    NSLog(@">>[GTSdk ReceivePayload]:%@, taskId: %@, msgId :%@", msg, taskId, msgId);
}

/** SDK收到sendMessage消息回調(diào) */
- (void)GeTuiSdkDidSendMessage:(NSString *)messageId result:(int)result {
    // 頁(yè)面顯示:上行消息結(jié)果反饋
    NSString *record = [NSString stringWithFormat:@"Received sendmessage:%@ result:%d", messageId, result];
    NSLog(@"%@", record);
}

/** SDK遇到錯(cuò)誤回調(diào) */
- (void)GeTuiSdkDidOccurError:(NSError *)error {
    // 頁(yè)面顯示:個(gè)推錯(cuò)誤報(bào)告,集成步驟發(fā)生的任何錯(cuò)誤都在這里通知软驰,如果集成后笤喳,無(wú)法正常收到消息,查看這里的通知碌宴。
    NSLog(@"%@",error.localizedDescription);
//    [_viewController logMsg:[NSString stringWithFormat:@">>>[GexinSdk error]:%@", [error localizedDescription]]];
}

/** SDK運(yùn)行狀態(tài)通知 */
- (void)GeTuiSDkDidNotifySdkState:(SdkStatus)aStatus {
    // 頁(yè)面顯示更新通知SDK運(yùn)行狀態(tài)
    NSLog(@"%d", aStatus);
//    [_viewController updateStatusView:self];
}

/** SDK設(shè)置推送模式回調(diào)  */
- (void)GeTuiSdkDidSetPushMode:(BOOL)isModeOff error:(NSError *)error {
    // 頁(yè)面顯示錯(cuò)誤信息
    if (error) {
        NSLog(@"%@", error.localizedDescription);
        return;
    }
    
//    [_viewController logMsg:[NSString stringWithFormat:@">>>[GexinSdkSetModeOff]: %@", isModeOff ? @"開(kāi)啟" : @"關(guān)閉"]];
//    
//    // 頁(yè)面更新按鈕事件
//    UIViewController *vc = _naviController.topViewController;
//    if ([vc isKindOfClass:[ViewController class]]) {
//        ViewController *nextController = (ViewController *) vc;
//        [nextController updateModeOffButton:isModeOff];
//    }
}

@end

至此杀狡,基本的代碼集成就完成了,可以進(jìn)行相關(guān)的測(cè)試了贰镣。

二呜象、個(gè)推平臺(tái)需要做的準(zhǔn)備

1.上傳APNs的.p12證書(shū),關(guān)于證書(shū)的請(qǐng)求和轉(zhuǎn)換可以參考如下鏈接:http://docs.getui.com/mobile/ios/apns/

2.完成證書(shū)上傳后碑隆,我們就可以運(yùn)行程序了恭陡。運(yùn)行程序我們需要拿到注冊(cè)推送服務(wù)器完成后的設(shè)備的:device token。

運(yùn)行程序按后的截圖如下:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末上煤,一起剝皮案震驚了整個(gè)濱河市休玩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖拴疤,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件永部,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡呐矾,警方通過(guò)查閱死者的電腦和手機(jī)苔埋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蜒犯,“玉大人组橄,你說(shuō)我怎么就攤上這事》K妫” “怎么了玉工?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)淘菩。 經(jīng)常有香客問(wèn)我遵班,道長(zhǎng),這世上最難降的妖魔是什么瞄勾? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮弥激,結(jié)果婚禮上进陡,老公的妹妹穿的比我還像新娘。我一直安慰自己微服,他們只是感情好趾疚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著以蕴,像睡著了一般糙麦。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丛肮,一...
    開(kāi)封第一講書(shū)人閱讀 51,292評(píng)論 1 301
  • 那天赡磅,我揣著相機(jī)與錄音,去河邊找鬼宝与。 笑死焚廊,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的习劫。 我是一名探鬼主播咆瘟,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼诽里!你這毒婦竟也來(lái)了袒餐?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎灸眼,沒(méi)想到半個(gè)月后卧檐,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡幢炸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年泄隔,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宛徊。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡佛嬉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闸天,到底是詐尸還是另有隱情暖呕,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布苞氮,位于F島的核電站湾揽,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏笼吟。R本人自食惡果不足惜库物,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贷帮。 院中可真熱鬧戚揭,春花似錦、人聲如沸撵枢。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)锄禽。三九已至潜必,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間沃但,已是汗流浹背磁滚。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宵晚,地道東北人恨旱。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像坝疼,于是被迫代替她去往敵國(guó)和親搜贤。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • 極光推送: 1.JPush當(dāng)前版本是1.8.2钝凶,其SDK的開(kāi)發(fā)除了正常的功能完善和擴(kuò)展外也緊隨蘋(píng)果官方的步伐仪芒,SD...
    Isspace閱讀 6,719評(píng)論 10 16
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,104評(píng)論 25 707
  • 前言 iOS10下唁影,遠(yuǎn)程推送發(fā)生了變化,新增的UserNotifications.framework將本地推送和遠(yuǎn)...
    夭Y夭閱讀 1,127評(píng)論 2 10
  • 剛看過(guò)新上映的《我不是潘金蓮》之后掂名,便對(duì)潘金蓮這個(gè)人物有些想法据沈,大家不要想污了,我不是對(duì)這個(gè)人有想法饺蔑,我只是對(duì)這個(gè)...
    請(qǐng)叫我三叔閱讀 712評(píng)論 4 4
  • 本已在腦海里消失的記憶因?yàn)檫@份作業(yè)全部恢復(fù)了出來(lái)锌介,這個(gè)記憶是我與大寶的,那個(gè)時(shí)候小寶兩個(gè)多月猾警,都是吃飽睡睡飽吃...
    時(shí)光漫步_dc5c閱讀 329評(píng)論 0 0