iOS10: 個推接入

個推的工作流程如下:?


推送流程

接入步驟

1. 證書創(chuàng)建

2. 推送平臺創(chuàng)建APP

3. 代碼搭建


證書創(chuàng)建

1. 要有開發(fā)證書( certificate + appID )

一般先創(chuàng)建AppID: 創(chuàng)建一個通配符: com.companyName.*


再創(chuàng)建Certificate: 使用上述AppID創(chuàng)建, CSR文件電腦創(chuàng)建, 下載下來雙擊.

開發(fā)證書

2. 創(chuàng)建推送證書( certificate + AppID )

還是先得創(chuàng)建一個AppID: 這里必須要創(chuàng)建"全名"ID, 不能使用通配符. 這時創(chuàng)建的時候要選擇Push Notification功能



記得勾選

創(chuàng)建完AppID之后, 發(fā)現(xiàn)Push Notification是黃色圖標的, 還欠配置. 完成下面一步之后就可以了.

還是在certificate里, 創(chuàng)建一個推送證書, 還是需要CSR文件, AppID則選擇剛剛創(chuàng)建的全名ID.?


推送證書

創(chuàng)建完成之后, 雙擊安裝證書, 并導出p12文件.(待會第二步消息平臺有用), 設(shè)置完密碼和名字之后, 把他放好待會用


導出p12文件

3. 創(chuàng)建DeviceID

UDID可以在iTunes獲取, 然后點擊下一步創(chuàng)建就好

4. 創(chuàng)建profile文件

certificate + AppID + DeviceID

certificate 選開發(fā)者證書(反正你選不了推送證書)

AppID 選全名ID

DeviceID全選都行

之后下載profile文件, 雙擊執(zhí)行

配置Xcode:

要打開Push Notification功能

打開Background Mode功能, 并勾選Notification和BackgroundFetch


2. 推送平臺創(chuàng)建APP



3. 代碼搭建

1. 初始化SDK

[GeTuiSdkstartSdkWithAppId:GETUI_APPIDappKey:GETUI_APPKEYappSecret:GETUI_APPSECRETdelegate:self];

APPKey, APPID, APPSecret 是在第二步消息推送平臺創(chuàng)建APP之后給的

2. 注冊遠程推送

自iOS10出來之后, 注冊遠程推送有3個方法, UNUserNotificationCenter , UIUserNotification, UIRemoteNotification

UNUserNotificationCenter: (iOS10之后)

UNUserNotificationCenter*center = [UNUserNotificationCentercurrentNotificationCenter];

center.delegate=self;

[centerrequestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSoundcompletionHandler:^(BOOLgranted,NSError*_Nullableerror) {

if(!error) {

NSLog(@"authorize succeed!");

}

}];

[[UIApplicationsharedApplication]registerForRemoteNotifications];

UIUserNotification:(iOS8之后)

UIUserNotificationTypetype = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeBadge;

UIUserNotificationSettings*setting = [UIUserNotificationSettingssettingsForTypes:typecategories:nil];

[[UIApplicationsharedApplication]registerForRemoteNotifications];

[[UIApplicationsharedApplication]registerUserNotificationSettings:setting];

UIRemoteNotification:?

UIRemoteNotificationTypetype = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;

[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:type];

注冊成功之后, 會走APPDelegate的一個回調(diào)didRegisterForRemoteNotificationsWithDeviceToken

在這個回調(diào)里, 需要執(zhí)行下面代碼: 處理DeviceToken

NSString*deviStr = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];

deviStr = [deviStrstringByReplacingOccurrencesOfString:@" "withString:@""];

[GeTuiSdkregisterDeviceToken:deviStr];

因為開啟了后臺模式里的BackgroundFetch, 所以下面AppDelegate回調(diào)會執(zhí)行

- (void)application:(UIApplication*)application performFetchWithCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

[GeTuiSdkresume];

completionHandler(UIBackgroundFetchResultNewData);

}

之后還要處理接收到通知的回調(diào):?

接受通知的回調(diào)有兩種, 要看app是通過那個API完成遠程推送的注冊, 是UNUserNotificationCenter還是UIUserNotification

若是前者, 則使用以下回調(diào)處理點擊通知:?

- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler {

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

[GeTuiSdkhandleRemoteNotification:response.notification.request.content.userInfo];

completionHandler();

若是后者, 則使用以下回調(diào)處理點擊通知:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler {

//個推處理數(shù)據(jù),收到推送

NSLog(@"AppDelegateDidReceive: %@", userInfo);

[GeTuiSdkhandleRemoteNotification:userInfo];

completionHandler(UIBackgroundFetchResultNewData);

}


以上便完成個推的接入

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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