1、导入 SDK
Cocoapods 导入,通过 Cocoapods 下载地址:pod 'Push'
2、使用 Xcode 8 及以上环境开发--开启通知
开启3、将以下代码添加到 AppDelegate.m 引用头文件的位置。
//引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用 idfa 功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>
4、添加代理 <JPUSHRegisterDelegate>
5、初始化APNS代码
将以下代码添加到 -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Required
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
JPUSHRegisterEntity * entity =[[JPUSHRegisterEntity alloc]init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
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];
// Optional
//获取IDFA
//如需使用IDFA功能请添加此代码并在初始化方法的advertisingIdentifier参数中填写对应值
NSString *advertisingId =[[[ASIdentifierManager sharedManager]advertisingIdentifier]UUIDString];
// Required
// init Push
// notice: 2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil
//如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:advertisingId];
部分参数说明:
IDFAappKey:(极光推送创建应用的appKey)
channel:自定义如:App Store。
apsForProduction:
1.3.1 版本新增,用于标识当前应用所使用的 APNs 证书环境。
0(默认值)表示采用的是开发证书,1 表示采用生产证书发布应用。
注:此字段的值要与Build Settings的Code Signing配置的证书环境一致。
advertisingIdentifier: IDFA
6、传deviceToken
-(void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required -注册DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
7、注册APNS失败方法
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
NSLog(@"did Fail To Register For Remote Notifications With Error: %@",error);
}
8、添加处理 APNs 通知回调方法,在 AppDelegate.m 实现该回调方法并添加回调方法中的代码
#pragma mark- JPUSHRegisterDelegate
// iOS 12 Support
-(void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification{
if(notification &&[notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]){
//从通知界面直接进入应用
}else{
//从通知设置界面进入应用
}
}
// 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 iOS 6
[JPUSHService handleRemoteNotification:userInfo];
}
更详细的配置文档参考 iOS 极光推送SDK集成指南
网友评论