最近研究蘋(píng)果原生apns,極光推送在此不詳細(xì)解釋了抚垃,具體查看極光文檔
?極光推送傳送門(mén)
原生APNS,iOS 代碼如下
在AppDelegate 里面加入如下代碼
#import "AppDelegate.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import
#endif
#define IOS_VERSION? [UIDevice currentDevice].systemVersion.floatValue
#import
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
? ?if(IOS_VERSION>=10.0) {
? ? ? ?UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter];
? ? ? ?[centersetDelegate:self];
? ? ? ?UNAuthorizationOptions type = UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert;
????????[centerrequestAuthorizationWithOptions:type completionHandler:^(BOOL granted, NSError * _Nullable error) {
????????if(granted) {
????????????NSLog(@"注冊(cè)成功");
????????}else{
????????????NSLog(@"注冊(cè)失敗");
????????}
? ? ? }];
? ?}elseif(IOS_VERSION>=8.0){
? ? ? ? UIUserNotificationTypenotificationTypes =UIUserNotificationTypeBadge|
? ? ? ? UIUserNotificationTypeSound |
? ? ? ? UIUserNotificationTypeAlert;
? ? ? ? UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
? ? ? ? [applicationregisterUserNotificationSettings:settings];
? ? }else{//ios8一下
? ? ? ? UIRemoteNotificationTypenotificationTypes =UIRemoteNotificationTypeBadge|
? ? ? ? UIRemoteNotificationTypeSound |
? ? ? ? UIRemoteNotificationTypeAlert;
? ? ? ? [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
? ? }
? ? // 注冊(cè)獲得device Token
? ? [applicationregisterForRemoteNotifications];
? ? return YES;
}
// 將得到的deviceToken傳給SDK
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken{
? ? NSString*deviceTokenStr = [[[[deviceTokendescription]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stringByReplacingOccurrencesOfString:@"<" withString:@""]
?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stringByReplacingOccurrencesOfString:@">" withString:@""]
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? stringByReplacingOccurrencesOfString:@" " withString:@""];
? ? NSLog(@"deviceTokenStr:\n%@",deviceTokenStr);
//向后太發(fā)送獲取的token,使用工具測(cè)試,發(fā)送廣播不需要
//后臺(tái)指定設(shè)備token 定向推送
//[ self postToken:deviceTokenStr];
}
// 注冊(cè)deviceToken失敗
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error{
? ?NSLog(@"error -- %@",error);
}
//在前臺(tái)
- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
? ?// 需要執(zhí)行這個(gè)方法趟大,選擇是否提醒用戶(hù)鹤树,有Badge、Sound逊朽、Alert三種類(lèi)型可以設(shè)置
? ? NSLog(@"前臺(tái) %@",notification.request.content.userInfo);
? ? completionHandler(UNNotificationPresentationOptionBadge|
? ? ? ? ? ? ? ? ? ? ?UNNotificationPresentationOptionSound|
? ? ? ? ? ? ? ? ? ? ?UNNotificationPresentationOptionAlert);
}
? ? //處理推送過(guò)來(lái)的數(shù)據(jù)
- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
? ? NSLog(@"后臺(tái) %@",response.notification.request.content.userInfo);
? ? completionHandler();
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*_Nonnull)userInfo fetchCompletionHandler:(void(^_Nonnull)(UIBackgroundFetchResult))completionHandler{
? NSLog(@"didReceiveRemoteNotification:%@",userInfo);
? ? /*
? ? UIApplicationStateActive 應(yīng)用程序處于前臺(tái)
?? ? UIApplicationStateBackground 應(yīng)用程序在后臺(tái)罕伯,用戶(hù)從通知中心點(diǎn)擊消息將程序從后臺(tái)調(diào)至前臺(tái)
?? ? UIApplicationStateInactive 用用程序處于關(guān)閉狀態(tài)(不在前臺(tái)也不在后臺(tái)),用戶(hù)通過(guò)點(diǎn)擊通知中心的消息將客戶(hù)端從關(guān)閉狀態(tài)調(diào)至前臺(tái)
?? ? */
? ? //應(yīng)用程序在前臺(tái)給一個(gè)提示特別消息
? ? if(application.applicationState==UIApplicationStateActive) {
? ? ? ? //應(yīng)用程序在前臺(tái)
????}else{
????//其他兩種情況叽讳,一種在后臺(tái)程序沒(méi)有被殺死追他,另一種是在程序已經(jīng)殺死坟募。用戶(hù)點(diǎn)擊推送的消息進(jìn)入app的情況處理。
}
? ?completionHandler(UIBackgroundFetchResultNewData);
}
向服務(wù)器傳token
-(void)postToken:(NSString*)token{
? ? AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
? ? manager.responseSerializer = [AFHTTPResponseSerializer serializer];
? ? manager.requestSerializer.timeoutInterval = 10; ? ? ? ? ? ? [managerPOST:@""parameters:@{}progress:^(NSProgress*_NonnulluploadProgress) {
? ?}success:^(NSURLSessionDataTask*_Nonnulltask,id? _NullableresponseObject) {
? ? ? ? NSError*error =nil;
? ? ? ? id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves|NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];
? ? ? ? if(error) {
? ? ? ? ? ?NSLog(@"%@",obj);
? ? ? ? }
? ? }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
? ?}];
}
@end
Easy APNs Provider - 推送測(cè)試工具
?mac下載地址
按照步驟邑狸,
1配置token懈糯,
2選擇測(cè)試或生產(chǎn)證書(shū)(.cer)格式,這里不需要p12的
3測(cè)試證書(shū)就選擇gateway.sandbox.push.apple.com ? 生產(chǎn)證書(shū)就選擇gateway.push.apple.com
4發(fā)送推送