個推的工作流程如下:?
接入步驟
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)建, 下載下來雙擊.
2. 創(chuàng)建推送證書( certificate + AppID )
還是先得創(chuàng)建一個AppID: 這里必須要創(chuàng)建"全名"ID, 不能使用通配符. 這時創(chuàng)建的時候要選擇Push Notification功能
創(chuàng)建完AppID之后, 發(fā)現(xiàn)Push Notification是黃色圖標(biāo)的, 還欠配置. 完成下面一步之后就可以了.
還是在certificate里, 創(chuàng)建一個推送證書, 還是需要CSR文件, AppID則選擇剛剛創(chuàng)建的全名ID.?
創(chuàng)建完成之后, 雙擊安裝證書, 并導(dǎo)出p12文件.(待會第二步消息平臺有用), 設(shè)置完密碼和名字之后, 把他放好待會用
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. 注冊遠(yuǎn)程推送
自iOS10出來之后, 注冊遠(yuǎn)程推送有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完成遠(yuǎn)程推送的注冊, 是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);
}
以上便完成個推的接入