APNs流程:
- 到developer.apple.com 去申请开发者资格
- 到苹果开发者官网 去申请一个 应用程序的 唯一标识, 即 bundle identify
- 凭借这个唯一标识, 可以在官网上申请两个证书.(一个安装在客户端的机器上, 一个是给服务器的)
- 当用户手机上的该软件启动之后, (首次)会向用户提出推送申请.
用户通过申请, 则软件会自动向苹果服务器发出请求, 申请此设备在此应用下的唯一标识. 苹果服务器根据证书来验证是否合法. - 如果验证合法, 则苹果服务器会传递一个deviceToken给此设备. 本质上就是一个32位 16进制 字符串
- 你需要把这个字符串 通过 你的服务器给定的接口. 传递过去. 服务器就会进行保存和关联.
- 服务器 把要发送给用户的消息+用户的deviceToken+证书 一起提交给苹果服务器
- 苹果服务器 根据证书 首先验证是否合法. 合法之后, 通过deviceToken找到对应的设备. 发送消息.
JPush创建应用 --> 获取Appkey
1.上传APNs证书 PS:APNs证书获取流程见2018年iOS APP提交上架流程中4.4
2.JPush上Bundle ID 必须跟项目中info.plist中的一致,否则无法推送
配置工程:
1.导入SDK — > 推荐Cocoapods导入
2.Capabilities —> 开启Application Target的Capabilities —> Push Notifications
3.允许Xcode7及以上支持Http传输方法
选择1:根据域名配置
3.1 Info.plist添加NSAppTransportSecurity(BOOL类型)
3.2 NSAppTransportSecurity添加一个NSExceptionDomains (Dictionary类型)
3.3 把需要支持的域添加给NSExceptionDomains (jpush.cn作为key,类型为Dictionary类型)
3.4 每个域下面需要设置2个属性: NSIncludesSubdomains、NSExceptionAllowsInsecureHTTPLoads (均为BOOL类型,值为YES)
选择2:全局配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
初始化设置及功能实现
AppDelegate.h 文件:
#import <JPUSHService.h>
#define JMESSAGE_APPKEY @""
#define CHANNEL @"Publish channel"
@interface AppDelegate : UIResponder <JPUSHRegisterDelegate>
@end
AppDelegate.m 文件:
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的头文件(可选)
#import <AdSupport/AdSupport.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//1.初始化APNs代码
//Required
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
// [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//2.初始化JPush代码
// 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]方式初始化。
// channel: 指明应用程序包的下载渠道
// apsForProduction: 是否用于生产环境
[JPUSHService setupWithOption:launchOptions appKey:JMESSAGE_APPKEY
channel:CHANNEL
apsForProduction:YES
advertisingIdentifier:nil];
//2.1.9版本新增获取registration id block接口。JPush相关事件监听
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if(resCode == 0){
NSLog(@"registrationID获取成功:%@",registrationID);
}
else{
NSLog(@"registrationID获取失败,code:%d",resCode);
}
}];
return YES;
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 3.注册APNs成功并上报DeviceToken
// Required - 注册 DeviceToken
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
[JPUSHService resetBadge];
[application setApplicationIconBadgeNumber:0];
}
//4.处理APNs通知回调方法
#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];
}else {
// 本地通知
}
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | 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];
[self handleNotiMessage:userInfo];//处理消息内容
}else {
// 本地通知
}
completionHandler(); // 系统要求执行这个方法
}
// iOS 7,8,9 Support
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
[self handleNotiMessage:userInfo];//处理消息内容
completionHandler(UIBackgroundFetchResultNewData);
}
//处理消息内容 根据后台返回数据进行对应处理
- (void) handleNotiMessage:(NSDictionary *)userInfo {
if (userInfo[@"extrasData"] != nil) {
NSString *extrasData = userInfo[@"extrasData"];
NSDictionary *extras = [NSString parseJSONStringToNSDictionary:extrasData];
NSString *type = [userInfo valueForKey:@"extrasType"];
if ([type isEqualToString:@"1"]) {
} else if ([type isEqualToString:@"5"]) {
}
}
NSInteger badge = [userInfo[@"aps"][@"badge"] integerValue];
if (badge == 0) {
[JPUSHService setBadge:0];
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
} else {
badge --;
[JPUSHService setBadge:badge];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
}
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString *alert = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"推送消息"
message:alert
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
NSInteger badge = [userInfo[@"aps"][@"badge"] integerValue];
if (badge == 0) {
[JPUSHService resetBadge];
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
} else {
badge --;
[JPUSHService setBadge:badge];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
}
// Required,For systems with less than or equal to iOS6
[JPUSHService handleRemoteNotification:userInfo];
}
推送消息给指定用户实现 -- > 设置别名alias
用户登录时设置别名
PS:alias不能设置nil或者空字符串@“” 建议设置为用户名
[JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
NSInteger ResCode = iResCode;
NSString *Alias = iAlias;//返回的状态码 0为成功
NSInteger iSeq = seq;
NSLog(@"%ld----%@=---%ld",ResCode,Alias,iSeq);
} seq:seq];
用户退出时删除别名
[JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
NSInteger ResCode = iResCode;
NSString *Alias = iAlias;//返回的状态码 0为成功
NSInteger iSeq = seq;
NSLog(@"%ld----%@=---%ld",ResCode,Alias,iSeq);
} seq:seq];