Xcode8 、iOS10的极光推送

一、申请证书

申请证书的过程在这里就不多说了,请自行查阅资料。
证书申请好并下载后,在极光的官网上传证书(p12文件)。

二、打开推送开关

Xcode8 、iOS10的极光推送_第1张图片
Xcode8 、iOS10的极光推送_第2张图片
勾选推送选项

然后我们会看到一个这样的文件出现了,这个不需要理它。

三、导入SDK并完善代码

准备工作做好了,就可以把下载好的SDK导入工程,开始写代码了。

Xcode8 、iOS10的极光推送_第3张图片

这时候可能会报错:


Xcode8 、iOS10的极光推送_第4张图片
导入SDK之后报错

需要添加一个库:libresolv.tbd ,就可以解决问题了:

Xcode8 、iOS10的极光推送_第5张图片
就是它

下面就开始写代码了(AppDelegate.m):

#import 

@interface AppDelegate ()

注册通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; //Required  
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0)  
    {  
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];  
        entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound;  
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    }  
    else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {  
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |  
                                                          UIUserNotificationTypeSound |  
                                                          UIUserNotificationTypeAlert) categories:nil];  
    }  
    else  
    {  
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |  
                                                          UIUserNotificationTypeSound |  
                                                          UIUserNotificationTypeAlert) categories:nil];  
    }  
    [JPUSHService setupWithOption:dict  
                           appKey:APPKEY  
                          channel:nil  
                 apsForProduction:NO  
            advertisingIdentifier:advertisingId];  
}

注册推送,推送是否成功:

//注册推送  
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
{  
    // Required  
    [JPUSHService registerDeviceToken:deviceToken];  
}  
  
//注册远程通知失败  
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {  
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);  
}  

接收推送

//iOS10 以前
//接收推送  
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
{  
    // Required  
    [JPUSHService handleRemoteNotification:userInfo];  
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
    NSLog(@"收到通知:%@", [self logDic:userInfo]);  
}  
//推送接收完成  
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {  
      
    // IOS 7 Support Required  
    [JPUSHService handleRemoteNotification:userInfo];  
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];  
    completionHandler(UIBackgroundFetchResultNewData);  
    NSLog(@"收到通知:%@", [self logDic:userInfo]);  
    _userInfo = userInfo;  
}  

#pragma mark- JPUSHRegisterDelegate  
// iOS 10 Support  
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {  
    NSDictionary * userInfo = notification.request.content.userInfo;  
    UNNotificationRequest *request = notification.request; // 收到推送的请求  
    UNNotificationContent *content = request.content; // 收到推送的消息内容  
    NSNumber *badge = content.badge;  // 推送消息的角标  
    NSString *body = content.body;    // 推送消息体  
    UNNotificationSound *sound = content.sound;  // 推送消息的声音  
    NSString *subtitle = content.subtitle;  // 推送消息的副标题  
    NSString *title = content.title;  // 推送消息的标题  
      
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {  
        NSLog(@"iOS10 前台收到远程通知:%@", [self logDic:userInfo]);  
    }  
    else {  
        // 判断为本地通知  
        NSLog(@"iOS10 前台收到本地通知:{\\nbody:%@,\\ntitle:%@,\\nsubtitle:%@,\\nbadge:%@,\\nsound:%@,\\nuserInfo:%@\\n}",body,title,subtitle,badge,sound,userInfo);  
    }  
    completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);  
    // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置  
}  
  
// 通知的点击事件  
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{  
      
    NSDictionary * userInfo = response.notification.request.content.userInfo;  
    UNNotificationRequest *request = response.notification.request; // 收到推送的请求  
    UNNotificationContent *content = request.content; // 收到推送的消息内容  
    NSNumber *badge = content.badge;  // 推送消息的角标  
    NSString *body = content.body;    // 推送消息体  
    UNNotificationSound *sound = content.sound;  // 推送消息的声音  
    NSString *subtitle = content.subtitle;  // 推送消息的副标题  
    NSString *title = content.title;  // 推送消息的标题  
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {  
        NSLog(@"iOS10 收到远程通知:%@", [self logDic:userInfo]);  
    }  
    else  
    {  
        // 判断为本地通知  
        NSLog(@"iOS10 收到本地通知:{\\nbody:%@,\\ntitle:%@,\\nsubtitle:%@,\\nbadge:%@,\\nsound:%@,\\nuserInfo:%@\\n}",body,title,subtitle,badge,sound,userInfo);  
    }  
    // 系统要求执行这个方法  
    completionHandler();  
}  

到此为止,iOS7-iOS10 的推送就做完了。

你可能感兴趣的:(Xcode8 、iOS10的极光推送)