首页 > 程序开发 > 移动开发 > IOS >

iOS极光推送集成

2016-04-27

iOS集成极光推送 一、配置APNs 苹果APNs(英文全称:Apple Push Notification Service) 1 配置开发证书 ![Uploading apns_13_803165 png …] 2 配置生产证书 和配置开发证书的流

一、配置APNs

苹果APNs(英文全称:Apple Push Notification Service)

1.配置开发证书

apns_1.png

apns_2.png

apns_4.png

apns_5.png

apns_6.png

apns_7.png

apns_8.png

apns_9.png

apns_10.png

apns_11.png
![Uploading apns_13_803165.png …]

2.配置生产证书

和配置开发证书的流程相同。

3.将配置好的证书导出为 .p12 文件

apns_13.png

apns_14.png

apns_15.png

一、注册极光推送帐号

可登录 极光推送官网 注册帐号。

注册成功后进入控制台

apns_12.png

apns_16.png

二、集成极光推送SDK

sdk地址

我使用的是JPush-iOS-SDK-2.1.0。下载的文件里有个pdf文件iOS+SDK+Integration+Guide.pdf,介绍了集成极光推送的详细代码。

在工程文件中,做如下设置:

apns_17.png

![Uploading apns_18_876090.png …]

下面是我写的代码:

AppDelegate.m文件


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //设置appKey
    [JPushConfig registerJPush:launchOptions];
    return YES;
}


#pragma mark 修改通知处理函数
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler {
    //推送消息携带 content-available: 1 是Background 运行的必须参数,如果不携带此字段则与iOS7 之前版本的普通推送一样。
    //清空角标
    [JPUSHService resetBadge];
    if (!userInfo) {
        completionHandler(UIBackgroundFetchResultNoData);
        return ;
    }
    completionHandler(UIBackgroundFetchResultNewData);
    //根据服务器设置的 userInfo 的格式,进行解析
}

// Required
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
}

// Required
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [JPUSHService handleRemoteNotification:userInfo];
    application.applicationIconBadgeNumber = 0;
}

- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
    [JPUSHService showLocalNotificationAtFront:notification identifierKey:nil];
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [application setApplicationIconBadgeNumber:0];
}

JPushConfig.m文件

/** 注册 JPush */
+(void)registerJPush:(NSDictionary *)launchOptions {
    // Required
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound |
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];
    } else {
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeBadge)
                                              categories:nil];
    }
    NSString *appKey = @"极光推送生成的KEY";
    NSString *channel = @"App Store";//发布渠道,可以随便定义
    BOOL isProduction = false;//这个要根据极光推送控制台里设置的是开发环境还是生产环境
    //Required
    [JPUSHService setupWithOption:launchOptions
                           appKey:appKey
                          channel:channel
                 apsForProduction:isProduction];
}

收到通知消息后,在AppDelegate.m跳转到指定界面的方法:

    UIViewController *vc = [[UIViewController alloc] init];
    [self.window.rootViewController presentViewController:vc animated:YES completion:nil];
相关文章
最新文章
热点推荐