極光推送小結 - iOS

此次即友盟分享小結(友盟分享小結 - iOS)之后對推送也進行了一版優化.此次分享內容依然基于已經成功集成 SDK 后 code 層級部分.
注:此次分享基于 SDK 3.1.0,若版本相差較大,僅供參考.

極光推送官方文檔:?https://docs.jiguang.cn/jpush/guideline/intro/

?

首先,為分享單獨創建了一個類,為了可以更加清晰的劃分其內容部分.

注:創建該子類后,切記將其頭文件引入到 AppDelegate 類中.

#import "AppDelegate.h"
// Push
#import "AppDelegate+JPush.h"

  

其次,將項目工程中配置相關配置.

?

最后,便是具體 code 相關內容,將申請的相關 key 預先設置成宏準備好,方便使用和變更時進行調用和更改.

一.聲明

優先將初始化的相關接口配置聲明準備好.

#import "AppDelegate.h"@interface AppDelegate (JPush)/**JPush 注冊@param launchOptions 啟動項*/
- (void)registerJPush:(NSDictionary *)launchOptions;@end

將子類中聲明的方法在 AppDelegate 的方法中調用.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// UserDefaults 相關[[DZSBUserInfo sharedInstance] loadCacheData];// CoreData 相關[[CoreDataManager sharedCoreDataManager] managedObjectContext];// 友盟相關[self umAssociatedDetailSettings];// Push[self registerJPush:launchOptions];// Root VC[self setRootViewController];return YES;
}

  

二.實現

1.引入所需的頭文件
2.實現聲明類中接口
3.實現具體方法和代理事件

#import "AppDelegate+JPush.h"
#import <JPUSHService.h>
// iOS10注冊APNs所需頭文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
// 如果需要使用idfa功能所需要引入的頭文件(可選)
#import <AdSupport/AdSupport.h>static NSString *appKey = JPush_APPKEY;
static NSString *channel = JPush_CHANNEL;// @"AppStore"
static BOOL isProduction = FALSE;@interface AppDelegate () <JPUSHRegisterDelegate>@end@implementation AppDelegate (JPush)- (void)registerJPush:(NSDictionary *)launchOptions {//Required//notice: 3.0.0及以后版本注冊可以這樣寫,也可以繼續用之前的注冊方式JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {// 可以添加自定義categories// NSSet<UNNotificationCategory *> *categories for iOS10 or later// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9}[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];// 注冊// apn 內容獲取:
//    NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];// 監聽自定義消息[kNotificationCenter addObserver:selfselector:@selector(networkDidReceiveMessage:)name:kJPFNetworkDidReceiveMessageNotificationobject:nil];// 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]方式初始化。
//    [JPUSHService setupWithOption:launchOptions
//                           appKey:appKey
//                          channel:channel
//                 apsForProduction:isProduction];[JPUSHService setupWithOption:launchOptionsappKey:appKeychannel:channelapsForProduction:isProductionadvertisingIdentifier:nil];[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {NSLog(@"JPush --- resCode : %d,registrationID: %@",resCode,registrationID);//        [JPUSHService setDebugMode];[JPUSHService setLogOFF];// 生成環境調用}];
}/**獲取自定義消息推送內容content:獲取推送的內容messageID:獲取推送的messageID(key為@"_j_msgid")extras:獲取用戶自定義參數customizeField1:根據自定義key獲取自定義的value@param notification 結構體*/
- (void)networkDidReceiveMessage:(NSNotification *)notification {/*接收消息樣式{content = "\U7529\U9505\U7ed9IOS\Uff01";extras =     {fFunPageUrl = "www.baidu.com";fType = 2;};title = "\U6d4b\U8bd5";}*/NSDictionary * userInfo = [notification userInfo];/** 消息標題*/NSString *content = [userInfo valueForKey:@"content"];/** 消息內容*/NSDictionary *extras = [userInfo valueForKey:@"extras"];NSString *messageID = [userInfo valueForKey:@"_j_msgid"];NSString *customizeField1 = [extras valueForKey:@"customizeField1"]; //服務端傳遞的Extras附加字段,key是自己定義的}#pragma mark - Delegate
/**Required - 注冊 DeviceToken注:JPush 3.0.9 之前的版本,必須調用此接口,注冊 token 之后才可以登錄極光,使用通知和自定義消息功能。從 JPush 3.0.9 版本開始,不調用此方法也可以登錄極光。但是不能使用APNs通知功能,只可以使用JPush自定義消息。@param application 應用@param deviceToken 標識*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {[JPUSHService registerDeviceToken:deviceToken];
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {// Required,For systems with less than or equal to iOS6// 取得 APNs 標準信息內容NSDictionary *aps = [userInfo valueForKey:@"aps"];NSString *content = [aps valueForKey:@"alert"]; //推送顯示的內容NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; //badge數量NSString *sound = [aps valueForKey:@"sound"]; //播放的聲音// 取得Extras字段內容NSString *customizeField1 = [userInfo valueForKey:@"customizeExtras"]; //服務端中Extras字段,key是自己定義的NSLog(@"JPush\ncontent =[%@], badge=[%ld], sound=[%@], customize field  =[%@]",content,(long)badge,sound,customizeField1);[JPUSHService handleRemoteNotification:userInfo];NSLog(@"JPush - Receive notice\n%@", userInfo);// iOS badge 清0application.applicationIconBadgeNumber = 0;
}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {// Required, iOS 7 Support[JPUSHService handleRemoteNotification:userInfo];// 應用正處理前臺狀態下,不會收到推送消息,因此在此處需要額外處理一下if (application.applicationState == UIApplicationStateActive) {UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"收到推送消息"message:userInfo[@"aps"][@"alert"]delegate:nilcancelButtonTitle:@"取消"otherButtonTitles:@"確定",nil];[alert show];}NSLog(@"JPush - Receive notice\n%@", userInfo);// 收到通知處理相關事項 contentType(系統消息 & 系統公告NSString *contentType = [NSString stringWithFormat:@"%@", [userInfo objectForKey:@"contentType"]];// 消息集合NSMutableDictionary *dicMessage = [[NSMutableDictionary alloc] init];if ([contentType isEqualToString:@""]) {//系統消息// do somethings} else if ([contentType isEqualToString:@""]){//系統公告// do somethings}// block 回調completionHandler(UIBackgroundFetchResultNewData);
}/**注冊 APNs 失敗@param application 應用@param error       異常*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(nonnull NSError *)error {//OptionalNSLog(@"JPush --- did Fail To Register For Remote Notifications With Error: %@\nLocalizedDescription: %@", error, error.localizedDescription);
}#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler  API_AVAILABLE(ios(10.0)){// RequiredNSDictionary * userInfo = notification.request.content.userInfo;if (@available(iOS 10.0, *)) {if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}} else {// Fallback on earlier versions}if (@available(iOS 10.0, *)) {completionHandler(UNNotificationPresentationOptionAlert);// 需要執行這個方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設置} else {// Fallback on earlier versions}
}// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler  API_AVAILABLE(ios(10.0)){// RequiredNSDictionary * userInfo = response.notification.request.content.userInfo;if (@available(iOS 10.0, *)) {if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {[JPUSHService handleRemoteNotification:userInfo];}} else {// Fallback on earlier versions}completionHandler();  // 系統要求執行這個方法
}@end

注:以上實現部分所分享的內容是針對集成后的基本對接部分,其中不包含接收消息后的具體業務邏輯,具體業務邏輯需要根據產品需求在代理回調部分進行單獨自行定制開發.

  

分享內容中可能存在的縮寫內容部分 code 如下:

#pragma mark - 縮寫
#define kApplication        [UIApplication sharedApplication]
#define kKeyWindow          [UIApplication sharedApplication].keyWindow
#define kAppDelegate        ((AppDelegate*)[UIApplication sharedApplication].delegate)
#define kUserDefaults       [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]

  

以上便是此次分享的全部內容,較為簡易的推送小結,具體還以實際需求為準,可以自行 diy 調整,希望對大家有所幫助,也希望大神多多指點共進步!

?

轉載于:https://www.cnblogs.com/survivorsfyh/p/9718602.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/537793.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/537793.shtml
英文地址,請注明出處:http://en.pswp.cn/news/537793.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

word去除所有的空行

申請軟著時&#xff0c;需要復制源代碼到word里。每行代碼不能有換行&#xff0c;要緊湊的80頁代碼。每頁要50~55行代碼。 字體可設置為&#xff1a;宋體&#xff0c;5號&#xff0c;行間距固定值12。 演示實例 去除下面word代碼里的空行 第一步&#xff1a; word顯示隱藏的…

c語言Wndproc未定義,為什么我的老是未定義

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓這是源代碼#includeLRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow){static TCHAR szAppName[] TEXT("HelloWin&qu…

spark on yarn

2019獨角獸企業重金招聘Python工程師標準>>> spark on yarn 軟件安裝 當前環境 hadoop環境搭建參考&#xff1a;hadoop集群安裝 hadoop2.6spark-2.2.0-bin-hadoop2.6.tgzscala-2.11.12安裝scala tar -zxvf scala-2.11.12.tgz vi /etc/profile 添加以下內容 export S…

如何查看SQL Server2000執行過的SQL語句

SQLServer事件探查器可以完整記錄SQL服務器執行過的SQL語句以及存儲過程等 下面是SQLServer事件探查器的使用方法&#xff1a; 1. 打開SQL Server 企業管理器。 2. 從“工具”菜單選擇“事件探查器”。 3. 當“事件探查器”主界面打開后&#xff0c;從“文件”菜單選擇“新跟蹤…

c語言鏈表不帶頭節點的排序,不帶頭結點的單向鏈表排序——插入排序(C語言)...

LinkList* LinkListInsertSort(LinkList* pHead){LinkList *pFirst (LinkList *)NULL; /* 原鏈表剩下未排序節點的頭指針 */LinkList *pCurrInsert (LinkList *)NULL; /* 無序鏈表中當前待插入節點 */LinkList *pPrev (LinkList *)NULL; /* 有序鏈表中插入位置的前一個節點 …

免費字體

若不想字體版權有問題&#xff0c;可以參考以下幾種完全免費的字體&#xff1a; 方正&#xff1a;方正黑體、方正書宋、方正仿宋、方正楷體 思源&#xff1a;思源黑體、思源柔黑體、思源宋體 站酷&#xff1a;站酷酷黑體、站酷高端黑體、站酷快樂體、站酷意大利體 。

【Python爬蟲學習筆記12】Ajax數據爬取簡介

有時候在我們設計利用requests抓取網頁數據的時候&#xff0c;會發現所獲得的結果可能與瀏覽器顯示給我們的不一樣&#xff1a;比如說有的信息我們通過瀏覽器可以顯示&#xff0c;但一旦用requests卻得不到想要的結果。這種現象是因為我們通過requests獲得的都是HTML源文檔&…

c語言的報告一,C語言實驗報告(一).doc

C語言實驗報告(一)C語言實驗報告(一)一、實驗目的掌握C語言中&#xff0c;基本的輸入輸出函數的使用方法。掌握printf中轉義字符’\t’&#xff0c;’\n’的用法。掌握賦值語句的用法。掌握算術表達式、賦值表達式的計算。掌握數學函數的使用。二、實驗內容從鍵盤輸入一個6位整…

數據挖掘——相似文章推薦

相似文章推薦&#xff1a;在用戶閱讀某篇文章時&#xff0c;為用戶推薦更多的與在讀文章內容相類似的文章 相關概念&#xff1a; 推薦(Recommended)&#xff1a;指介紹好的人或事物&#xff0c;希望被任用或接受。數據挖掘領域&#xff0c;推薦包括相似推薦和協同過濾推薦。 相…

win10投影無法正常使用:我們正在確認此功能 解決方法

鼠標移動到開始按鈕&#xff0c;右鍵----- windows powershell&#xff08;管理員&#xff09; 輸入命令&#xff1a; netsh winsock reset 然后重啟電腦&#xff0c;問題解決

青海師大c語言研究生專業課,2016年青海師范大學計算機應用技術C語言程序設計考研復試題庫...

一、選擇題1&#xff0e; 有如下程序:程序運行后的輸出結果是( )。答:C【解析】題目中判斷if 條件是否成立&#xff0c;后a 自增 得if 條件不成立執行else 函數體&#xff0c;輸出F 。最后執行語句故C 選項正確。 按照格式輸出2&#xff0e; 有如下程序&#xff1a;先取值為0, …

產品經理和項目經理的差別

原文地址&#xff1a;https://blog.csdn.net/verifocus/article/details/79219539 --------------------------------------------------------------------- 項目經理與產品經理的區別&#xff0c;用一句話概括就是&#xff1a;產品經理是做正確的事情&#xff0c;項目經理是…

c語言設計一個按時間片輪轉法實現處理器調度的程序,設計一個按時間片輪轉法實現處理器調度的程序...

實驗一處理器調度一、實習內容選擇一個調度算法&#xff0c;實現處理器調度。&#xff1b;二、實習目的在采用多道程序設計的系統中&#xff0c;往往有若干個進程同時處于就緒狀態。當就緒進程個數大于處理器數時&#xff0c;就必須依照某種策略來決定哪些進程優先占用處理器。…

Perl的淺拷貝和深度拷貝

首先是深、淺拷貝的概念&#xff1a; 淺拷貝&#xff1a;shallow copy&#xff0c;只拷貝第一層的數據。Perl中賦值操作就是淺拷貝深拷貝&#xff1a;deep copy&#xff0c;遞歸拷貝所有層次的數據&#xff0c;Perl中Clone模塊的clone方法&#xff0c;以及Storable的dclone()函…

程序員分析報告(2018)-總結篇

一、生活中的程序員 居住篇 在主要職業群體中&#xff0c;程序員更傾向于租房&#xff0c;20.9%的受訪程序員目前自己有房&#xff0c;此比例明顯低于其他職業。大概是因為程序員大部分還比較 年輕&#xff0c;傳說中的超高薪水并不能讓很多人擁有自己的房子而更長的工作…

linux lvm 查看,Linux LVM 詳解

邏輯卷管理LVM是一個多才多藝的硬盤系統工具。無論在Linux或者其他類似的系統&#xff0c;都是非常的好用。傳統分區使用固定大小分區&#xff0c;重新調整大小十分麻煩。但是&#xff0c;LVM可以創建和管理“邏輯”卷&#xff0c;而不是直接使用物理硬盤。可以讓管理員彈性的管…

cnblogs修改網站圖標icon

以下修改網絡地址即可 <script type"text/javascript" language"javascript">//Setting ico for cnblogsvar linkObject document.createElement(link);linkObject.rel "shortcut icon";linkObject.href "icon的網絡地址";do…

maven settings.xml國內倉庫配置

<?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/SETTINGS/1.…

智慧園區-樓宇建模技巧之【建樓篇】

一、國際慣例先上圖 二、有什么用&#xff1f;用什么搞的&#xff1f;花了多久&#xff1f; 用途 室內定位(會議室、停車位查找等)安防監控(直接定位到某個樓道的攝像頭拉取視頻流)各種傳感器數據三維可視化請問哪里可以買到呢(含笑半步癲2333) 我這里正好有一個。https://iot.…

android meta工具,android ota 升級包制作分析 (5 工具)

工具1 mkbootfsmkbootfs的源代碼在system/core/cpio中。??mkbootfs -f boot_filesystem_config.txt targetfiles/BOOT/RAMDISK | minigzip > ramdisk.img2 mkbootimgmkbootimg的源代碼在system/core/mkbootimg中。mkbootimg --kernel kernel --ramdisk ramdisk.img --outp…