#import "JPUSHService.h" #import <AdSupport/AdSupport.h> #ifdef NSFoundationVersionNumber_iOS_9_x_Max // 这里是iOS10需要用到的框架 #import <UserNotifications/UserNotifications.h> #endif
static NSString *JPushAppKey = @"6abc87b33b23d35b9c3b86e0";
static NSString *JPushChannel = @"Publish channel";
// static BOOL JPushIsProduction = NO;
#ifdef DEBUG
// 开发 极光FALSE为开发环境
static BOOL const JPushIsProduction = FALSE;
#else
// 生产 极光TRUE为生产环境
static BOOL const JPushIsProduction = TRUE;
#endif
[objc] view plain copy 在CODE上查看代码片派生到我的代码片
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 启动极光推送
// Required
// - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) // iOS10
{
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity *entity = [[JPUSHRegisterEntity alloc] init];
entity.types = (UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound);
[JPUSHService registerForRemoteNotificationConfig:entity delegate:target];
#endif
}
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];
}
// Required
// [JPUSHService setupWithOption:launchOptions]
// pushConfig.plist appKey
// 有广告符标识IDFA(尽量不用,避免上架审核被拒)
/*
NSString *JPushAdvertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
[JPUSHService setupWithOption:JPushOptions
appKey:JPushAppKey
channel:JPushChannel
apsForProduction:JPushIsProduction
advertisingIdentifier:JPushAdvertisingId];
*/
// 或无广告符标识IDFA(尽量不用,避免上架审核被拒)
[JPUSHService setupWithOption:options
appKey:JPushAppKey
channel:JPushChannel
apsForProduction:JPushIsProduction];
// 2.1.9版本新增获取registration id block接口。
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0)
{
// iOS10获取registrationID放到这里了, 可以存到缓存里, 用来标识用户单独发送推送
NSLog(@"registrationID获取成功:%@",registrationID);
[[NSUserDefaults standardUserDefaults] setObject:registrationID forKey:@"registrationID"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[JPUSHService registerDeviceToken:data];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificwationsWithError:(NSError *)error
{
NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
// apn 内容获取:
// 取得 APNs 标准信息内容
[JPUSHService handleRemoteNotification:dict];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
DLog(@"2-1 didReceiveRemoteNotification remoteNotification = %@", userInfo);
// apn 内容获取:
[JPUSHService handleRemoteNotification:dict];
completionHandler(UIBackgroundFetchResultNewData);
DLog(@"2-2 didReceiveRemoteNotification remoteNotification = %@", userInfo);
if ([userInfo isKindOfClass:[NSDictionary class]])
{
NSDictionary *dict = userInfo[@"aps"];
NSString *content = dict[@"alert"];
DLog(@"content = %@", content);
}
if (application.applicationState == UIApplicationStateActive)
{
// 程序当前正处于前台
}
else if (application.applicationState == UIApplicationStateInactive)
{
// 程序处于后台
}
}
#pragma mark - iOS10: 收到推送消息调用(iOS10是通过Delegate实现的回调)
#pragma mark- JPUSHRegisterDelegate
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
// 当程序在前台时, 收到推送弹出的通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler
{
NSDictionary *userInfo = notification.request.content.userInfo;
if ([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
{
[JPUSHService handleRemoteNotification:userInfo];
}
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
// completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
// 程序关闭后, 通过点击推送弹出的通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
NSDictionary *userInfo = response.notification.request.content.userInfo;
if ([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
{
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(); // 系统要求执行这个方法
}
#endif
/// 绑定别名(注意:1 登录成功或者自动登录后;2 去除绑定-退出登录后)
+ (void)JPushTagsAndAliasInbackgroundTags:(NSSet *)set alias:(NSString *)name
{
// 标签分组(表示没有值)
NSSet *tags = set;
// 用户别名(自定义值,nil是表示没有值)
NSString *alias = name;
NSLog(@"tags = %@, alias = %@(registrationID = %@)", tags, alias, [self registrationID]);
// tags、alias均无值时表示去除绑定
[JPUSHService setTags:tags aliasInbackground:alias];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有