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

首頁 > 系統 > iOS > 正文

詳細介紹iOS10推送的方法

2020-02-19 15:51:21
字體:
來源:轉載
供稿:網友

相信每一個iOS開發者都知道在更新iOS 10之后,IOS的推送也相應做了一些改動,現在武林技術頻道小編將詳細介紹iOS10推送的方法,有需要的朋友可以從中學習哦!

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

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

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

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

- (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得到消息內容

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

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

- (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 中加入一段自定義提示音文件。然后當通知到達時可以指定播放這個文件。必須為以下幾種數據格式:

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

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

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

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

<?php// devicetoken $deviceToken = '你的deviceToken';// 私鑰密碼,生成pem的時候輸入的$passphrase = '123456';// 定制推送內容,有一點的格式要求,詳情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( //這里需要特別注意,一個是開發推送的沙箱環境,一個是發布推送的正式環境,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);?>

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

superdanny@SuperDannydeMacBook-Pro$ php push.php 

結果為

Connected to APNSMessage successfully delivered

以上是武林技術頻道小編為大家搜集的詳細介紹iOS10推送的方法,希望對大家的學習有一定的幫助!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沅陵县| 东海县| 洛宁县| 施甸县| 栾城县| 邮箱| 齐齐哈尔市| 牙克石市| 兴化市| 正定县| 江油市| 乡宁县| 阿拉尔市| 新兴县| 保定市| 三江| 客服| 浦北县| 交口县| 桑日县| 莱西市| 鞍山市| 张家川| 安平县| 广南县| 合肥市| 射洪县| 阿勒泰市| 浮梁县| 衡东县| 宕昌县| 股票| 天气| 兴化市| 辽中县| 兰坪| 繁昌县| 凯里市| 平谷区| 太谷县| 会同县|