iOS开发(第三方使用)——极光推送SDK接入

来源:互联网 发布:手机助手软件 编辑:程序博客网 时间:2024/05/22 08:04
  1. pod ‘JPush’
  2. 添加Framework
    CFNetwork.framework
    CoreFoundation.framework
    CoreTelephony.framework
    SystemConfiguration.framework
    CoreGraphics.framework
    Foundation.framework
    UIKit.framework
    Security.framework
    libz.tbd (Xcode7以下版本是libz.dylib)
    AdSupport.framework (获取IDFA需要;如果不使用IDFA,请不要添加)
    UserNotifications.framework (Xcode8及以上)
    libresolv.tbd (JPush 2.2.0及以上版本需要, Xcode7以下版本是libresolv.dylib)

  3. 这里写图片描述

  4. AppDelegate.m代码
//推送#import <JPUSHService.h>// iOS10注册APNs所需头文件#ifdef NSFoundationVersionNumber_iOS_9_x_Max#import <UserNotifications/UserNotifications.h>#endif//JPUSHRegisterDelegate代理@interface AppDelegate ()<JPUSHRegisterDelegate>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//推送    [self registerRemoteNotification];    //测试    if([jgAPNSForProduction isEqualToString:@"0"]){        [JPUSHService setupWithOption:launchOptions appKey:jgAppKey                              channel:@"App Store"                     apsForProduction:NO                advertisingIdentifier:nil];    }else{//api打包        [JPUSHService setupWithOption:launchOptions appKey:jgAppKey                              channel:@"App Store"                     apsForProduction:YES                advertisingIdentifier:nil];    }    //获取registrationID通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getRegistrationID:) name:kJPFNetworkDidLoginNotification object:nil];    //获取透传信息通知    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkDidReceiveMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];    }//注册APNS- (void)registerRemoteNotification {    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) {    //可以添加自定义categories    [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |                                                      UIUserNotificationTypeSound |                                                      UIUserNotificationTypeAlert)                                          categories:nil];  }   else {    //categories 必须为nil    [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |                                                      UIRemoteNotificationTypeSound |                                                      UIRemoteNotificationTypeAlert)                                          categories:nil];}//registrationID与后台交互使用-(void)getRegistrationID:(NSNotification *)notification{    NSString *registrationID=[JPUSHService registrationID];//    NSLog(@"registrationID=%@",registrationID);    //[self sendRegistrationID];//把registrationID传给后台}//透传(即应用内推送)- (void)networkDidReceiveMessage:(NSNotification *)notification{//    NSLog(@"userinfo=%@",notification.userInfo);    //收到推送,自行处理}//token- (void)application:(UIApplication *)applicationdidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {    //注册 DeviceToken    [JPUSHService registerDeviceToken:deviceToken];}//APNS- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    // Required,For systems with less than or equal to iOS6    [JPUSHService handleRemoteNotification:userInfo];}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {    // IOS 7 Support Required    [JPUSHService handleRemoteNotification:userInfo];    completionHandler(UIBackgroundFetchResultNewData);}#pragma mark- JPUSHRegisterDelegate// 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();  // 系统要求执行这个方法}
0 0
原创粉丝点击