官方文档
cocoapods集成
# 极光推送
pod 'JPush'
添加头文件
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions
{
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
} else {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
// 启动SDK
[JPUSHService setupWithOption:launchOptions
appKey:JPUSH_APPKEY
channel:@"App Store"
apsForProduction:1
advertisingIdentifier:nil];
// release版本关闭Log打印
[JPUSHService setLogOFF];
return YES;
}
apsForProduction
1.3.1 版本新增,用于标识当前应用所使用的 APNs 证书环境。
0(默认值)表示采用的是开发证书,1 表示采用生产证书发布应用。
注:此字段的值要与 Build Settings的Code Signing 配置的证书环境一致。
AppDelegate代理方法
// 注册 APNs 成功并上报 DeviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
/// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
// 注册 APNs 失败接口
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
//Optional
LRLog(@"注册token失败:*****did Fail To Register For Remote Notifications With Error: %@", error);
}
// 收到推送通知 iOS 10 以后不需要调用该方法,极光集成了UNUserNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
JPUSHRegisterDelegate
// App在前台运行,收到推送消息
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler {
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
// 弹出通知框,不执行这段代码,不会在通知栏弹出提示框
completionHandler(UNNotificationPresentationOptionBadge|
UNNotificationPresentationOptionSound|
UNNotificationPresentationOptionAlert);
}
// 点击通知栏推送消息
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
{
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler();
}
// iOS 12 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification
{
if (notification && [notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
// 从通知界面直接进入应用
}else{
// 从通知设置界面进入应用
}
}
// 监测通知授权状态返回的结果
- (void)jpushNotificationAuthorization:(JPAuthorizationStatus)status withInfo:(NSDictionary *)info {
}
App在后台时收到推送通知自动在通知栏弹窗提示。
Api
设置别名(登录成功调用,用户id作为别名)
别名不支持中划线,恰巧我们的数据格式包含中划线,做了字符串转换。
有效的别名组成:字母(区分大小写)、数字、下划线、汉字、特殊字符@!#$&*+=.|
[JPUSHService setAlias:userId completion:nil seq:0];
删除别名(退出登录,不再接收推送)
[JPUSHService deleteAlias:nil seq:0];
设置服务器端角标
[JPUSHService setBadge:0];
设置本地角标
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];