JPush--推送

APNs流程:
  1. 到developer.apple.com 去申請開發(fā)者資格
  2. 到蘋果開發(fā)者官網(wǎng) 去申請一個 應(yīng)用程序的 唯一標識, 即 bundle identify
  3. 憑借這個唯一標識, 可以在官網(wǎng)上申請兩個證書.(一個安裝在客戶端的機器上, 一個是給服務(wù)器的)
  4. 當用戶手機上的該軟件啟動之后, (首次)會向用戶提出推送申請.
    用戶通過申請, 則軟件會自動向蘋果服務(wù)器發(fā)出請求, 申請此設(shè)備在此應(yīng)用下的唯一標識. 蘋果服務(wù)器根據(jù)證書來驗證是否合法.
  5. 如果驗證合法, 則蘋果服務(wù)器會傳遞一個deviceToken給此設(shè)備. 本質(zhì)上就是一個32位 16進制 字符串
  6. 你需要把這個字符串 通過 你的服務(wù)器給定的接口. 傳遞過去. 服務(wù)器就會進行保存和關(guān)聯(lián).
  7. 服務(wù)器 把要發(fā)送給用戶的消息+用戶的deviceToken+證書 一起提交給蘋果服務(wù)器
  8. 蘋果服務(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];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市奕翔,隨后出現(xiàn)的幾起案子裕寨,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件宾袜,死亡現(xiàn)場離奇詭異捻艳,居然都是意外死亡,警方通過查閱死者的電腦和手機庆猫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門认轨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人月培,你說我怎么就攤上這事嘁字。” “怎么了杉畜?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵纪蜒,是天一觀的道長纯续。 經(jīng)常有香客問我灭袁,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任举娩,我火速辦了婚禮,結(jié)果婚禮上铜涉,老公的妹妹穿的比我還像新娘遂唧。我一直安慰自己,他們只是感情好盖彭,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布召边。 她就那樣靜靜地躺著,像睡著了一般片挂。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上沪饺,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天整葡,我揣著相機與錄音肝谭,去河邊找鬼。 笑死攘烛,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的鼠次。 我是一名探鬼主播芋齿,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼觅捆,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了栅炒?” 一聲冷哼從身側(cè)響起掂摔,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤乙漓,失蹤者是張志新(化名)和其女友劉穎叭披,沒想到半個月后玩讳,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡皱坛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年剩辟,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片熊户。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡嚷堡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蝌戒,到底是詐尸還是另有隱情沼琉,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布友鼻,位于F島的核電站闺骚,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏虫碉。R本人自食惡果不足惜胸梆,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绞惦。 院中可真熱鬧洋措,春花似錦杰刽、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春珠月,著一層夾襖步出監(jiān)牢的瞬間楔敌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工庆聘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留勺卢,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓澳腹,卻偏偏與公主長得像杨何,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子羊娃,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容