关于 ios 推送(极光推送基本流程)| 推送证书-> 极光官网 -> 代码流程 -> 发送推送

ios推送(极光推送)

前期准备:

极光官网:https://www.jiguang.cn  没帐号的自行注册

苹果开发者帐号官网:https://developer.apple.com  帐号自行登录

正文:

1.制作推送证书

1.1.打开苹果开发者帐号官网,帐号登录。

1.2.点击 ,进入Certificates, Identifiers & Profiles 页面

1.3.点击,找到与项目对应的Bundle ID(如果没有,点击右上角+,创建一个)。

      1.3.1.有Bundle ID。找到对应的ID,点击编辑edit,将Push Notifications勾选上,点击保存。

      1.3.2.没有Bundle Id,点击+添加。填写App ID Description,Bundle ID(和项目的Bundle ID一致),勾选push Notifications。

        

 1.4.完成后,打开对应的Bundle ID,显示:(黄色表示证书没做,绿色表示证书OK) 

1.5.创建测试(开发)推送证书和推送正式证书。点击 ,右上角+号。

点击下一步:,点击继续,,上传CSR文件,

注:CSR的制作:

      1.打开mac电脑 『钥匙串访问』

      2.点击    ,填写相应邮箱名字等点击保存,即为CSR文件。

1.6.分别点击下载,asp.cer为正式推送证书,aps_development.cer为测试开发推送证书。双击安装。over。

2.极光官网创建相应推送应用

2.1.打开极光官网,选择   , 点击创建应用,,选择推送设置。

2.2.选择证书配置,上传生产和开发证书p12文件  ,保存。结果这样:。over。

注:证书p12文件的制作:

       1.打开『钥匙串访问』,点击我的证书,找到(步骤1最后双击安装的)

       2.右击选择导出,设置名称和密码,保存。

3.添加代码流程

3.1.项目设置

      3.1.1.打开推送开关:项目-》Capabilities-》  

      3.1.2.打开Info.plist,添加  (App Transport Security Settings/Exception Domains/jpush.cn/NSExceptionAllowsInsecureHTTPLoads/NSIncludesSubdomains)

3.2.代码添加

     3.2.1.接入SDK:pod 'JPush'

     3.2.2.定义:

#define JPushAppKey @"xxxxxxx"            //极光官网创建完应用后(步骤2),提供APPKey
#define JPushChannel @"AppStorexxxx"      //指明应用程序包的下载渠道
#define JPushIsProduction NO              //是否是生产环境

    3.2.3.XHJPush.m

#pragma mark - 注册JPush
+(void)registerJPush:(NSDictionary *)launchOptions delegate:(id<JPUSHRegisterDelegate>)delegate{
    //Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
        JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
        [JPUSHService registerForRemoteNotificationConfig:entity delegate:delegate];
    }
    else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(JPAuthorizationOptionBadge |
                                                          JPAuthorizationOptionSound |
                                                          JPAuthorizationOptionAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(JPAuthorizationOptionBadge |
                                                          JPAuthorizationOptionSound |
                                                          JPAuthorizationOptionAlert)
                                              categories:nil];
    }
    [JPUSHService setupWithOption:launchOptions appKey:JPushAppKey channel:JPushChannel apsForProduction:JPushIsProduction];
}

#pragma mark -注册设备处
+ (void)registerDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
    return;
}

#pragma mark - ios7以后,才有completion,否则传nil
+ (void)handleRemoteNotification:(NSDictionary *)userInfo completion:(void (^)(UIBackgroundFetchResult))completion {
    [JPUSHService handleRemoteNotification:userInfo];
    if (completion) {
        completion(UIBackgroundFetchResultNewData);
    }
    return;
}

     3.2.4.AppDelegate+JPush.m

#pragma mark - 注册
-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //推送(注册)
    [XHJPush registerJPush:launchOptions delegate:self];
}
#pragma mark - 注册 DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // Required
    [XHJPush registerDeviceToken:deviceToken];
}
#pragma mark - 接收到远程通知之后处理
// 接收到远程通知之后 <ios6
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required,For systems with less than or equal to iOS6
    [XHJPush handleRemoteNotification:userInfo completion:nil];
}
// 接收到远程通知之后 >ios7
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // IOS 7 Support Required
    [XHJPush handleRemoteNotification:userInfo completion:completionHandler];
}
#pragma mark - 推送失败处理
//获取 deviceToken 失败后 远程推送(极光推送)打开失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    //Optional
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

代理JPUSHRegisterDelegate

#pragma mark - JPUSHRegisterDelegate
// iOS 10 Support,前台收到通知,后台不会执行这个方法
#pragma mark - 前台收到通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    //completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert(上面角标)三种类型可以选择设置
    // 通知内容为:notification.request.content.body
    NSLog(@"***********%@",notification.request.content.body);
}

#pragma mark - iOS 10 Support,用户点击了通知进入app
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];//userInfo消息数据,包含jump。
        if ([UIApplication sharedApplication].applicationState == UIApplicationStateInactive ) {
            [self jumpToViewctroller:userInfo];//内部页面跳转
            [UIApplication sharedApplication].applicationIconBadgeNumber = 0; //本地消息清0
            [JPUSHService setBadge:0];//极光消息清0
        }
    }
    completionHandler();  // 系统要求执行这个方法
}

     3.2.5.AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //注册极光推送
    [self JPushApplication:application didFinishLaunchingWithOptions:launchOptions];
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    //app角标清零
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
    [JPUSHService setBadge:0];//清空JPush服务器中存储的badge值
}

4.发送推送内容

4.1.打开极光官网,选择推送-》发送通知

4.2.编辑推送内容,目标平台,立即(定时发送),在『可选设置』中,可以填写title(如果推送内容有标题),

badge选项写+1(这样,app角标会自动累加1)。

4.3.点击立即发送后,手机app会收到所编辑的推送通知。

      4.3.1.手机相应应用关闭在后台,能收到推送通知。

      4.3.2.手机相应应用打开在前台,根据代码『#pragma mark - 前台收到通知』,可以选择是否接受展示通知,获取通知内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值