国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS推送的那些事

2020-07-26 03:28:21
字體:
供稿:網(wǎng)友

直接切入主題,講講如何模擬推送以及處理推送消息。在進入主題之前,我先說幾個關(guān)鍵流程:
1、建Push SSL Certification(推送證書)
2、OS客戶端注冊Push功能并獲得DeviceToken
3、用Provider向APNS發(fā)送Push消息
4、OS客戶端接收處理由APNS發(fā)來的消息
推送流程圖:

Provider:就是為指定iOS設(shè)備應(yīng)用程序提供Push的服務(wù)器。如果iOS設(shè)備的應(yīng)用程序是客戶端的話,那么Provider可以理解為服務(wù)端(推送消息的發(fā)起者)
APNs:Apple Push Notification Service(蘋果消息推送服務(wù)器)
Devices:iOS設(shè)備,用來接收APNs下發(fā)下來的消息
Client App:iOS設(shè)備上的應(yīng)用程序,用來接收APNs下發(fā)的消息到指定的一個客戶端app(消息的最終響應(yīng)者)
1、取Device token
App 必須要向 APNs 請求注冊以實現(xiàn)推送功能,在請求成功后,APNs 會返回一個設(shè)備的標識符即 DeviceToken 給 App,服務(wù)器在推送通知的時候需要指定推送通知目的設(shè)備的 DeviceToken。在 iOS 8 以及之后,注冊推送服務(wù)主要分為四個步驟:

  • 使用 registerUserNotificationSettings:注冊應(yīng)用程序想要支持的推送類型
  • 通過調(diào)用 registerForRemoteNotifications方法向 APNs 注冊推送功能
  • 請求成功時,系統(tǒng)會在應(yīng)用程序委托方法中返回 DeviceToken,請求失敗時,也會在對應(yīng)的委托方法中給出請求失敗的原因。
  • 將 DeviceToken 上傳到服務(wù)器,服務(wù)器在推送時使用。

上述第一個步驟注冊的 API 是 iOS 8 新增的,因此在 iOS 7,前兩個步驟需更改為 iOS 7 中的 API。
DeviceToken 有可能會更改,因此需要在程序每次啟動時都去注冊并且上傳到你的服務(wù)器端。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {  NSLog(@"Requesting permission for push notifications..."); // iOS 8  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:   UIUserNotificationTypeAlert | UIUserNotificationTypeBadge |   UIUserNotificationTypeSound categories:nil];  [UIApplication.sharedApplication registerUserNotificationSettings:settings]; } else {  NSLog(@"Registering device for push notifications..."); // iOS 7 and earlier  [UIApplication.sharedApplication registerForRemoteNotificationTypes:   UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |   UIRemoteNotificationTypeSound]; } return YES;}- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)settings{ NSLog(@"Registering device for push notifications..."); // iOS 8 [application registerForRemoteNotifications];}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token{ NSLog(@"Registration successful, bundle identifier: %@, mode: %@, device token: %@",  [NSBundle.mainBundle bundleIdentifier], [self modeString], token);}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ NSLog(@"Failed to register: %@", error);}- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)notification completionHandler:(void(^)())completionHandler{ NSLog(@"Received push notification: %@, identifier: %@", notification, identifier); // iOS 8 completionHandler();}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification{ NSLog(@"Received push notification: %@", notification); // iOS 7 and earlier}- (NSString *)modeString{#if DEBUG return @"Development (sandbox)";#else return @"Production";#endif}

2、處理推送消息
1)、程序未啟動,用戶接收到消息。需要在AppDelegate中的didFinishLaunchingWithOptions得到消息內(nèi)容

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //... NSDictionary *payload = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; if (payload) {  //... } //...}

2)、程序在前臺運行,接收到消息不會有消息提示(提示框或橫幅)。當程序運行在后臺,接收到消息會有消息提示,點擊消息后進入程序,AppDelegate的didReceiveRemoteNotification函數(shù)會被調(diào)用,消息做為此函數(shù)的參數(shù)傳入

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)payload { NSLog(@"remote notification: %@",[payload description]); NSString* alertStr = nil;   NSDictionary *apsInfo = [payload objectForKey:@"aps"];  NSObject *alert = [apsInfo objectForKey:@"alert"];  if ([alert isKindOfClass:[NSString class]])  {    alertStr = (NSString*)alert;  }  else if ([alert isKindOfClass:[NSDictionary class]])  {    NSDictionary* alertDict = (NSDictionary*)alert;    alertStr = [alertDict objectForKey:@"body"];  }   application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];   if ([application applicationState] == UIApplicationStateActive && alertStr != nil)  {  UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pushed Message" message:alertStr delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];  [alertView show];  }}

3、義通知提示音
你可以在 App 的 Bundle 中加入一段自定義提示音文件。然后當通知到達時可以指定播放這個文件。必須為以下幾種數(shù)據(jù)格式:

  • Linear PCM
  • MA4(IMA/ADPCM)
  • μLaw
  • aLaw

你可以將它們打包為aiff、wav或caf文件。自定義的聲音文件時間必須小于 30秒,如果超過了這個時間,將被系統(tǒng)聲音代替。
4、Payload
Payload 是通知的一部分,每一條推送通知都包含一個 Payload。它包含了系統(tǒng)提醒用戶通知到達的方式,還可以添加自定義的數(shù)據(jù)。即通知主要傳遞的數(shù)據(jù)為 Payload。
Payload 本身為 JSON 格式的字符串,它內(nèi)部必須要包含一個鍵為 aps 的字典。aps 中可以包含以下字段中的一個或多個:
alert:其內(nèi)容可以為字符串或者字典,如果是字符串,那么將會在通知中顯示這條內(nèi)容
badge:其值為數(shù)字,表示當通知到達設(shè)備時,應(yīng)用的角標變?yōu)槎嗌佟H绻麤]有使用這個字段,那么應(yīng)用的角標將不會改變。設(shè)置為 0 時,會清除應(yīng)用的角標。
sound:指定通知展現(xiàn)時伴隨的提醒音文件名。如果找不到指定的文件或者值為 default,那么默認的系統(tǒng)音將會被使用。如果為空,那么將沒有聲音。
content-available:此字段為 iOS 7 silent remote notification 使用。不使用此功能時無需包含此字段。
如果需要添加自定義的字段,就讓服務(wù)器的小伙伴們跟aps同一層級添加一個數(shù)組(以Json為例):

{  "aps" : {"alert" : "This is the alert text", "badge" : 1, "sound" :"default" },  "server" : {"serverId" : 1, "name" : "Server name"}}

這樣收到的 Payload 里面會多一個 server 的字段。
5、模擬推送
現(xiàn)在常用的后臺server中,一般將推送證書以及推送證書的私鑰導(dǎo)出p12交給后臺人員即可。
生成PHP需要的Pem證書
6、PHP有點調(diào)皮,還需要轉(zhuǎn)換成pem
準備:
1)、蘋果服務(wù)器證書端設(shè)置正確!打包證書、描述文件正確!!
2)、下載推送證書(cer格式),導(dǎo)入keyChain,保證私鑰存在,不存在去找創(chuàng)建這個證書的電腦要一份過來。
3)、從鑰匙庫導(dǎo)出的~~根證書~~(推送證書)私鑰(p12格式)
第三步根證書的私鑰這里是一個坑!因為一個App的推送證書的創(chuàng)建可以和根證書創(chuàng)建的電腦不同,也就是keyChain產(chǎn)生的certSigningRequest不一樣,所以私鑰也是不一樣的,在這里生成Pem時,注意要使用推送證書的私鑰!
操作過程:
A.把推送證書(.cer)轉(zhuǎn)換為.pem文件,執(zhí)行命令:
openssl x509 -in 推送證書.cer -inform der -out 推送證書.pem
B.把推送證書導(dǎo)出的私鑰(.p12)文件轉(zhuǎn)化為.pem文件:
openssl pkcs12 -nocerts -out 推送證書私鑰.pem -in 推送證書私鑰.p12 
C.對生成的這兩個pem文件再生成一個pem文件,來把證書和私鑰整合到一個文件里:
cat 推送證書.pem 推送證書私鑰.pem >PHPPush.pem
然后把這個PHPPush.pem給后臺基友們,就可以下班啦。
當然測試推送也比較麻煩,需要模擬真實的推送環(huán)境,一般需要后臺提供幫助,但是遇到一些后臺同事,他們有強烈地信仰著鄙視鏈的話,很鄙視iOS,心里早就稱呼你“死前段”多年了,還那么多事……
所以關(guān)于調(diào)試推送,這里有兩種方式實現(xiàn)自推!不麻煩別人。
模擬推送:通過終端推送

<?php// devicetoken $deviceToken = '你的deviceToken';// 私鑰密碼,生成pem的時候輸入的$passphrase = '123456';// 定制推送內(nèi)容,有一點的格式要求,詳情Apple文檔$message = array( 'body'=>'你收到一個新訂單');$body['aps'] = array( 'alert' => $message, 'sound' => 'default', 'badge' => 100, );$body['type']=3;$body['msg_type']=4;$body['title']='新訂單提醒';$body['msg']='你收到一個新消息';$ctx = stream_context_create();stream_context_set_option($ctx, 'ssl', 'local_cert', 'push.pem');//記得把生成的push.pem放在和這個php文件同一個目錄stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);$fp = stream_socket_client( //這里需要特別注意,一個是開發(fā)推送的沙箱環(huán)境,一個是發(fā)布推送的正式環(huán)境,deviceToken是不通用的 'ssl://gateway.sandbox.push.apple.com:2195', $err, //'ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);if (!$fp) exit("Failed to connect: $err $errstr" . PHP_EOL);echo 'Connected to APNS' . PHP_EOL;$payload = json_encode($body);$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;$result = fwrite($fp, $msg, strlen($msg));if (!$result) echo 'Message not delivered' . PHP_EOL;else echo 'Message successfully delivered' . PHP_EOL;fclose($fp);?>

將上面的代碼復(fù)制,保存成push.php
然后根據(jù)上面生成PHP需要的Pem證書的步驟生成push.pem
兩個文件放在同一目錄
執(zhí)行下面的命令

superdanny@SuperDannydeMacBook-Pro$ php push.php 

結(jié)果為

Connected to APNSMessage successfully delivered

本文已被整理到了《iOS推送教程》,歡迎大家學(xué)習(xí)閱讀。

以上就是關(guān)于IOS推送的那些事,希望對大家的學(xué)習(xí)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 张掖市| 绥德县| 安龙县| 皋兰县| 上栗县| 贺州市| 双辽市| 邢台市| 台南市| 乐安县| 洱源县| 汉阴县| 鸡泽县| 克东县| 阿城市| 拜城县| 澜沧| 高淳县| 杭锦后旗| 府谷县| 惠安县| 汉沽区| 汶川县| 沂水县| 澎湖县| 遵化市| 上思县| 来宾市| 犍为县| 库尔勒市| 霍州市| 普兰店市| 莆田市| 苏尼特左旗| 建阳市| 晴隆县| 临江市| 永新县| 华蓥市| 织金县| 五峰|