iOS 极光推送(有证书的情况下10分钟集成)

学而时习之,不亦悦乎,大家好,我是张杰。

关于iOS推送有很多选择,有原生的,有三方的,目前很多公司为了保存三方统一(接口,苹果,安卓)基本上都使用第三方(大厂除外)。

今天我们主要来讲一下10分钟集成极光推送,并告诉你如何和后台(接口)联调。

首先:你需要会做推送证书(如果不会没关系,点击链接查看我的推送证书制作流程)。

https://www.jianshu.com/p/705dcea5566d

然后:集成到项目。

https://docs.jiguang.cn//jpush/client/iOS/ios_guide_new/ 极光文档,建议用pod

然后:导入头文件

#import 

// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#endif

然后:设置代理

@interface AppDelegate ()

然后:设置代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


//Required
//notice: 3.0.0 及以后版本注册可以这样写,也可以继续用之前的注册方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    // 可以添加自定义 categories
    // NSSet *categories for iOS10 or later
    // NSSet *categories for iOS8 and iOS9
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];


//极光推送
// Optional
// 获取 IDFA
// 如需使用 IDFA 功能请添加此代码并在初始化方法的 advertisingIdentifier 参数中填写对应值
//    NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// Required
// init Push
// notice: 2.1.5 版本的 SDK 新增的注册方法,改成可上报 IDFA,如果没有使用 IDFA 直接传 nil
[JPUSHService setupWithOption:launchOptions appKey:@"4569e88018f759b5b752d7b0"
                      channel:@"AppStore"
             apsForProduction:false
        advertisingIdentifier:nil];


[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
    if(resCode == 0){
        NSLog(@"registrationID获取成功:%@",registrationID);
        [[NSUserDefaults standardUserDefaults ] setObject:registrationID forKey:@"registrationID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else{
        NSLog(@"registrationID获取失败,code:%d",resCode);
    }
}];


if ([[NSUserDefaults standardUserDefaults] objectForKey:@"token"]) {
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *mian = [mainSB instantiateViewControllerWithIdentifier:@"Main"];
    self.window.rootViewController = mian;
}else{
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"login" bundle:nil];
    UITabBarController *mian = [mainSB instantiateViewControllerWithIdentifier:@"login"];
    self.window.rootViewController = mian;
}

return YES;
}

- (void)application:(UIApplication *)application

didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

pragma mark- JPUSHRegisterDelegate

 // iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    //从通知界面直接进入应用
}else{
    //从通知设置界面进入应用
}
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有 Badge、Sound、Alert 三种类型可以选择设置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
    [JPUSHService handleRemoteNotification:userInfo];
    
     [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    
    NSDictionary *dic = userInfo;
//        NSDictionary *apns = dic[@"aps"];
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    
    NSString *category = [NSString stringWithFormat:@"%@",dic[@"category"]];
    
    NSString *sn = [NSString stringWithFormat:@"%@",dic[@"sn"]];
    
    if ([category containsString:@"null"]) {
        
    }else {
        if ([category isEqualToString:@"1"]) {
            lockRecordViewController *vc = [[lockRecordViewController alloc] init];
            MianNavigationViewController *nvc = [[MianNavigationViewController alloc] initWithRootViewController:vc];
            vc.SN = sn;
            vc.push = @"push";
            self.window.rootViewController = nvc;
            
        }else if ([category isEqualToString:@"2"]){
            SafetyWarningViewController *vc = [[SafetyWarningViewController alloc] init];
            MianNavigationViewController *nvc = [[MianNavigationViewController alloc] initWithRootViewController:vc];
            vc.SN = sn;
            vc.push = @"push";
            self.window.rootViewController = nvc;
//消息体,点击消息进入APP根据后台返回的参数跳到指定页面
        }
    }
    
}
completionHandler();  // 系统要求执行这个方法
}

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

// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

// Required, For systems with less than or equal to iOS 6
[JPUSHService handleRemoteNotification:userInfo];
}

然后:设置推送打开

WeChateb746481e3e4dfd47c357f5e10bba3aa.png

然后:生成ID 传给后台,后台根据这个ID给你推送内容,这个上面代码已经有了

[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
    if(resCode == 0){
        NSLog(@"registrationID获取成功:%@",registrationID);
        [[NSUserDefaults standardUserDefaults ] setObject:registrationID forKey:@"registrationID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    else{
        NSLog(@"registrationID获取失败,code:%d",resCode);
    }
}];

然后:消息数字清除。

[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];

最后:打开极光后台,测试推送,

https://www.jiguang.cn/jpush2/#/app/4569e88018f759b5b752d7b0/push_form/notification

如果有错误或者还有其他问题,可以联系我:[email protected],谢谢

你可能感兴趣的:(iOS 极光推送(有证书的情况下10分钟集成))