APNs流程:
- 到developer.apple.com 去申請開發(fā)者資格
- 到蘋果開發(fā)者官網(wǎng) 去申請一個 應(yīng)用程序的 唯一標識, 即 bundle identify
- 憑借這個唯一標識, 可以在官網(wǎng)上申請兩個證書.(一個安裝在客戶端的機器上, 一個是給服務(wù)器的)
- 當用戶手機上的該軟件啟動之后, (首次)會向用戶提出推送申請.
用戶通過申請, 則軟件會自動向蘋果服務(wù)器發(fā)出請求, 申請此設(shè)備在此應(yīng)用下的唯一標識. 蘋果服務(wù)器根據(jù)證書來驗證是否合法. - 如果驗證合法, 則蘋果服務(wù)器會傳遞一個deviceToken給此設(shè)備. 本質(zhì)上就是一個32位 16進制 字符串
- 你需要把這個字符串 通過 你的服務(wù)器給定的接口. 傳遞過去. 服務(wù)器就會進行保存和關(guān)聯(lián).
- 服務(wù)器 把要發(fā)送給用戶的消息+用戶的deviceToken+證書 一起提交給蘋果服務(wù)器
- 蘋果服務(wù)器 根據(jù)證書 首先驗證是否合法. 合法之后, 通過deviceToken找到對應(yīng)的設(shè)備. 發(fā)送消息.
JPush創(chuàng)建應(yīng)用 --> 獲取Appkey
1.上傳APNs證書 PS:APNs證書獲取流程見2018年iOS APP提交上架流程中4.4
2.JPush上Bundle ID 必須跟項目中info.plist中的一致,否則無法推送
配置工程:
1.導(dǎo)入SDK — > 推薦Cocoapods導(dǎo)入
2.Capabilities —> 開啟Application Target的Capabilities —> Push Notifications
3.允許Xcode7及以上支持Http傳輸方法
選擇1:根據(jù)域名配置
3.1 Info.plist添加NSAppTransportSecurity(BOOL類型)
3.2 NSAppTransportSecurity添加一個NSExceptionDomains (Dictionary類型)
3.3 把需要支持的域添加給NSExceptionDomains (jpush.cn作為key矩乐,類型為Dictionary類型)
3.4 每個域下面需要設(shè)置2個屬性: NSIncludesSubdomains龄句、NSExceptionAllowsInsecureHTTPLoads (均為BOOL類型,值為YES)
選擇2:全局配置
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
初始化設(shè)置及功能實現(xiàn)
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及以后版本注冊可以這樣寫散罕,也可以繼續(xù)用之前的注冊方式
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功能請?zhí)砑哟舜a并在初始化方法的advertisingIdentifier參數(shù)中填寫對應(yīng)值
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
// Required
// init Push
// notice: 2.1.5版本的SDK新增的注冊方法分歇,改成可上報IDFA,如果沒有使用IDFA直接傳nil
// 如需繼續(xù)使用pushConfig.plist文件聲明appKey等配置內(nèi)容欧漱,請依舊使用[JPUSHService setupWithOption:launchOptions]方式初始化卿樱。
// channel: 指明應(yīng)用程序包的下載渠道
// apsForProduction: 是否用于生產(chǎn)環(huán)境
[JPUSHService setupWithOption:launchOptions appKey:JMESSAGE_APPKEY
channel:CHANNEL
apsForProduction:YES
advertisingIdentifier:nil];
//2.1.9版本新增獲取registration id block接口。JPush相關(guān)事件監(jiān)聽
[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通知回調(diào)方法
#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); // 需要執(zhí)行這個方法繁调,選擇是否提醒用戶,有Badge靶草、Sound蹄胰、Alert三種類型可以選擇設(shè)置
}
// 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];//處理消息內(nèi)容
}else {
// 本地通知
}
completionHandler(); // 系統(tǒng)要求執(zhí)行這個方法
}
// 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];//處理消息內(nèi)容
completionHandler(UIBackgroundFetchResultNewData);
}
//處理消息內(nèi)容 根據(jù)后臺返回數(shù)據(jù)進行對應(yīng)處理
- (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];
}
推送消息給指定用戶實現(xiàn) -- > 設(shè)置別名alias
用戶登錄時設(shè)置別名
PS:alias不能設(shè)置nil或者空字符串@“” 建議設(shè)置為用戶名
[JPUSHService setAlias:alias completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
NSInteger ResCode = iResCode;
NSString *Alias = iAlias;//返回的狀態(tài)碼 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;//返回的狀態(tài)碼 0為成功
NSInteger iSeq = seq;
NSLog(@"%ld----%@=---%ld",ResCode,Alias,iSeq);
} seq:seq];