#pragma mark - 判断是不是发布环境
#ifdef DEBUG
#define isProduct FALSE
#else
#define isProduct TRUE
#endif
.h文件如下:
static NSString *appKey = @"xxxxxxxxxxxxxxxx";
static NSString *channel = @"App Store";
static BOOL isProduction = isProduct;//0(默认值)表示采用的是开发证书,1 表示采用生产证书发布应用
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
.m文件如下:
// 引入 JPush 功能所需头文件
#import "JPUSHService.h"
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
服从<JPUSHRegisterDelegate>,然后在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions文件中如下:
//jpush
[self jPush:launchOptions];
//iOS 10以下程序在关闭状态收到通知,只有点击才能显示
if(CurrentDeviceVersion<10.0){
[self showNotificationsAPNS:launchOptions];
}
实现方法如下:
#pragma mark 极光推送
- (void)jPush:(NSDictionary *)launchOptions{
// 3.0.0及以后版本注册
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
if (@available(iOS 12.0, *)) {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
} else {
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
// if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
// NSSet<UNNotificationCategory *> *categories;
// entity.categories = categories;
// }
// else {
// NSSet<UIUserNotificationCategory *> *categories;
// entity.categories = categories;
// }
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//如不需要使用IDFA,advertisingIdentifier 可为nil
[JPUSHService setupWithOption:launchOptions appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
//2.1.9版本新增获取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0){
NSLog(@"registrationID获取成功:%@",registrationID);
}
else{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
}
#pragma mark iOS 10以下程序在关闭状态收到通知,只有点击才能显示
- (void)showNotificationsAPNS:(NSDictionary *)launchOptions{
NSDictionary* pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (pushInfo)
{
NSDictionary *apsInfo = [pushInfo objectForKey:@"aps"];
if(apsInfo)
{
//推送内容展示
NSString *subtitle = [apsInfo objectForKey:@"alert"]; // 推送的消息
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
message:subtitle
delegate:self
cancelButtonTitle:@"好的"
otherButtonTitles:nil, nil];
[test show];
}
}
}
最后在AppDelegate.m文件中的代理方法下面添加JPush的代理方法,如下:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
[application setApplicationIconBadgeNumber:0]; //清除角标
[JPUSHService setBadge:0];//同样的告诉极光角标为0了
[application cancelAllLocalNotifications];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[application setApplicationIconBadgeNumber:0]; //清除角标
[JPUSHService setBadge:0];//同样的告诉极光角标为0了
[application cancelAllLocalNotifications];
}
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]);
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:
(void (^)(UIBackgroundFetchResult))completionHandler {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS7及以上系统,收到通知");
//推送内容展示
NSString *subtitle = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"]; // 推送的消息
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
message:subtitle
delegate:self
cancelButtonTitle: @"好的"
otherButtonTitles:nil, nil];
[test show];
completionHandler(UIBackgroundFetchResultNewData);
}
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#pragma mark- JPUSHRegisterDelegate
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler API_AVAILABLE(ios(10.0)){
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 前台收到远程通知");
}
else {
// 判断为本地通知
NSLog(@"iOS10 前台收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
NSDictionary * userInfo = response.notification.request.content.userInfo;
UNNotificationRequest *request = response.notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSNumber *badge = content.badge; // 推送消息的角标
NSString *body = content.body; // 推送消息体
UNNotificationSound *sound = content.sound; // 推送消息的声音
NSString *subtitle = content.subtitle; // 推送消息的副标题
NSString *title = content.title; // 推送消息的标题
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[JPUSHService handleRemoteNotification:userInfo];
NSLog(@"iOS10 收到远程通知");
//推送内容展示
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
message:body
delegate:self
cancelButtonTitle:@"好的"
otherButtonTitles:nil, nil];
[test show];
}
else {
// 判断为本地通知
NSLog(@"iOS10 收到本地通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@\n}",body,title,subtitle,badge,sound,userInfo);
}
completionHandler(); // 系统要求执行这个方法
}
#endif
#ifdef __IPHONE_12_0
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center openSettingsForNotification:(UNNotification *)notification API_AVAILABLE(ios(10.0)){
if (notification) {
//从通知界面关闭-配置直接进入应用
NSDictionary * userInfo = notification.request.content.userInfo;
UNNotificationRequest *request = notification.request; // 收到推送的请求
UNNotificationContent *content = request.content; // 收到推送的消息内容
NSString *body = content.body; // 推送消息体
//推送内容展示
UIAlertView *test = [[UIAlertView alloc] initWithTitle:@"消息"
message:body
delegate:self
cancelButtonTitle:@"好的"
otherButtonTitles:nil, nil];
[test show];
}else{
//从通知界面-设置界面进入应用
}
}
#endif