您的位置:首页 > 移动开发 > IOS开发

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

2016-09-06 17:18 615 查看
pod ‘JPush’

添加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)



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 *)application
didRegisterForRemoteNotificationsWithDeviceToken:(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();  // 系统要求执行这个方法
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  极光推送