快速集成极光分享

2018-09-28  本文已影响0人  DSMars

第一步

当然是证书的问题,这里我就直接以开发证书为例,如果是生产证书,就直接自己再生成一个生产证书,搞成P12文件,提交到极光推送的控制台。

第二步 导入sdk

1:下载JPush的SDK:SDK下载地址

配push证书:此步骤直接看极光的 文档即可,写得很详细

第三步 创建项目

9936500-bb04f036d0978734.png 9936500-269ca65748301dc2.png 9936500-faad57bc56d83179.png

第四步 进入项目的appdelegate里面,首先导入头文件和遵循代理


// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 100000
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate ()<JPUSHRegisterDelegate>

五: 在didFinishLaunchingWithOptions方法中配置

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //极光推送

    [selfjpushInitWith:launchOptions];

    application.applicationIconBadgeNumber = 0;

}

- (void)jpushInitWith:(NSDictionary*)launchOptions

{

    //notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式

     JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];

    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;

    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];

    //初始化JPush
    [JPUSHServicesetupWithOption:launchOptionsappKey:@"你的key值"

                          channel:@"App Store"

                 apsForProduction:0

            advertisingIdentifier:nil];

    //app未运行时,收到推送消息
    NSDictionary *resultDic = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
    if(resultDic) {//推送进入APP
        NSLog(@"app未启动,推送进入,直接显示预警界面");
//        [self SetMainTabbarController2]; //相应界面跳转方法
    }else{//正常进入APP

    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
        //可以添加自定义categories
        [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
                                                          UIUserNotificationTypeSound|
                                                          UIUserNotificationTypeAlert)
                                              categories:nil];

    }else{
        //categories 必须为nil
        [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                          UIRemoteNotificationTypeSound |
                                                          UIRemoteNotificationTypeAlert)
                                              categories:nil];
    }
}

第六步 实现通知和协议方法

#pragma mark - 接收通知
// 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];
    }
    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 iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}

#pragma mark 注册通知
//注册APNs成功并上报DeviceToken
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
    [JPUSHServiceregisterDeviceToken:deviceToken];
}

//注冊消息推送失败
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {

}
上一篇下一篇

猜你喜欢

热点阅读