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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

IOS本地,APNS遠(yuǎn)程推送(具體過(guò)程)

2019-11-14 18:11:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

添加本地推送

///本地添加  

  1. -(void)addLocalPushNotification:(UIButton*)sender;  
  2. {  
  3.       
  4.       
  5.     NSLog(@"%s",__FUNCTION__);  
  6.     UILocalNotification* localNotification=[[UILocalNotification alloc]init];  
  7.       
  8.     if (localNotification) {  
  9.         //設(shè)置時(shí)間當(dāng)前加20秒  
  10.         NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];  
  11.         /*推送時(shí)區(qū)設(shè)置:從網(wǎng)上搜到 
  12.         timeZone是UILocalNotification激發(fā)時(shí)間是否根據(jù)時(shí)區(qū)改變而改變,如果設(shè)置為nil的話,那么UILocalNotification將在一段時(shí)候后被激發(fā),而不是某一個(gè)確切時(shí)間被激發(fā)。*/  
  13.         localNotification.timeZone=[NSTimeZone defaultTimeZone];  
  14.         ///推送時(shí)間設(shè)置  
  15.         localNotification.fireDate=pushDate;  
  16.         //時(shí)間間隔,若不設(shè)置將只會(huì)推送1次  
  17.         localNotification.repeatInterval=kCFCalendarUnitDay;  
  18.         //推送時(shí)的聲音,(若不設(shè)置的話系統(tǒng)推送時(shí)會(huì)無(wú)聲音)  
  19.         localNotification.soundName=UILocalNotificationDefaultSoundName;  
  20.         //推送的文字信息(若不設(shè)置,推送中心中不顯示文字,有聲音提示前提是設(shè)置有聲音)  
  21.         localNotification.alertBody=@"Hello world";  
  22.         //推送時(shí)小圖標(biāo)的設(shè)置,PS:這個(gè)東西不知道還有啥用  
  23.         localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];  
  24.           
  25.         ///這個(gè)東西,到時(shí)用于定位是哪個(gè)notification,以便取消用  
  26.         NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];  
  27.         localNotification.userInfo=infoDic;  
  28.           
  29.         //講推送設(shè)置以及信息加入  
  30.         UIapplication* app=[UIApplication sharedApplication];  
  31.         BOOL status=YES;  
  32.         for (UILocalNotification* notification in app.scheduledLocalNotifications) {  
  33.             if ([notification.userInfo objectForKey:@"key"]) {  
  34.                 status=NO;  
  35.             }  
  36.         }  
  37.           
  38.         if (status) {  
  39.             //加入推送(只能加入一次)  
  40.             [app scheduleLocalNotification:localNotification];  
  41.         }  
  42.           
  43.   
  44.           
  45.         NSLog(@"%@",app.scheduledLocalNotifications);  
  46.     }  
  47. }  

 

 

取消本地推送 
  1. ///本地移除  
  2. -(void)removeLocalPushNotification:(UIButton*)sender  
  3. {  
  4.     NSLog(@"%s",__FUNCTION__);  
  5.     UIApplication* app=[UIApplication sharedApplication];  
  6.     //獲取當(dāng)前應(yīng)用所有的通知  
  7.     NSArray* localNotifications=[app scheduledLocalNotifications];  
  8.   
  9.     if (localNotifications) {  
  10.           
  11.         for (UILocalNotification* notification in localNotifications) {  
  12.   
  13.             NSDictionary* dic=notification.userInfo;  
  14.               
  15.             if (dic) {  
  16.                 NSString* key=[dic objectForKey:@"key"];  
  17.                 if ([key isEqualToString:@"name"]) {  
  18.                     //取消推送 (指定一個(gè)取消)  
  19.                     [app cancelLocalNotification:notification];  
  20.                       
  21.                     break;  
  22.                 }  
  23.             }  
  24.   
  25.         }  
  26.     }  
  27.     //取消當(dāng)前應(yīng)用所有的推送  
  28.     //[app cancelAllLocalNotifications];  
  29.       
  30.       
  31. }  


 

 

 

 

 

遠(yuǎn)程推送

 

當(dāng)服務(wù)端遠(yuǎn)程向APNS推送至一臺(tái)離線的設(shè)備時(shí),蘋(píng)果服務(wù)器Qos組件會(huì)自動(dòng)保留一份最新的通知,等設(shè)備上線后,Qos將把推送發(fā)送到目標(biāo)設(shè)備上

 

客戶端需要注意的

 bundle ID與App Id一致

設(shè)備Token能正常獲取

若為沙盒測(cè)試,證書(shū)得使用developer的

 

單設(shè)備

如上圖所示:我們的服務(wù)端將需要推送的相關(guān)信息提交到APNS(Apple Push Notification Service),由APNS在Push服務(wù)IOS設(shè)備列表中找到對(duì)應(yīng)的設(shè)備,并將信息推到終端上,終端上再推到客戶端APP上

多設(shè)備

 

 

 

流程大概是這樣的

1.生成CertificateSigningRequest.certSigningRequest文件

2.將CertificateSigningRequest.certSigningRequest上傳進(jìn)developer,導(dǎo)出.cer文件

3.利用CSR導(dǎo)出P12文件

4.需要準(zhǔn)備下設(shè)備token值(無(wú)空格)

5.使用OpenSSL合成服務(wù)器所使用的推送證書(shū)

 

1.打開(kāi)鑰匙串,在右上角選擇(鑰匙串訪問(wèn)->證書(shū)助理->從證書(shū)頒發(fā)機(jī)構(gòu)請(qǐng)求證書(shū))

 

生成 CertificateSigningRequest.certSigningRequest

 

以下信息填寫(xiě)號(hào)后,保存到對(duì)應(yīng)位置

 

 

2.進(jìn)入developer.apple.com中 上傳CertificateSigningRequest.certSigningRequest并保存cer文件

(1)

(2)選擇類(lèi)型為 推送服務(wù)--沙盒測(cè)試用

 

(3)選中對(duì)應(yīng)的APP ID,別忘了,項(xiàng)目配置文件中的Bundle ID與其一致

(4)選擇保存路徑

(5)選擇上傳文件 CertificateSigningRequest.certSigningRequest

(6)保存cer文件,并雙擊添加進(jìn)鑰匙串

 

(7)新建一個(gè)PRovisioning Profiles

 

 

選中與前面一致的 App Id

 

選中剛才新建的certificates

 

選擇可調(diào)試設(shè)備

 

保存provisioning文件,并將其加入設(shè)備中

 

通過(guò)OPENSSL文件合并

1.在鑰匙串->證書(shū) 找到剛才所添加進(jìn)去的證書(shū) 右鍵導(dǎo)出p12

2.進(jìn)入終端 ,將aps_development.cer轉(zhuǎn)成PushChatCert.pem(openssl x509 -in aps_development.cer -inform der  -out PushChatCert.pem)

3.openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12  生成p12私鑰 .pem文件(需設(shè)置密碼,服務(wù)端推送時(shí)要用)

4.利用PushChatCert.pem和新生成的PushChatKey.pem合成一個(gè)新的p12文件(這個(gè)p12是提供給服務(wù)器推送用的)(

openssl pkcs12 -export -in PushChatCert.pem -inkey PushChatKey.pem -certfile CertificateSigningRequest.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12

 

合成php所用的PEM文件

 

  1.   openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
  2.  openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
  3. cat PushChatCert.pem PushChatKey.pem > newck.pem

 

 

 

 

 

代碼實(shí)現(xiàn)如下

 

注冊(cè)推送通知

 

  1. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:  
  2. (UIRemoteNotificationTypeAlert|  
  3.  UIRemoteNotificationTypeBadge|  
  4.  UIRemoteNotificationTypeSound)];  


 

在AppDelegate中加入以下幾個(gè)代理方法

 

  1. ///Token值成功獲取的時(shí)候走的是這個(gè)方法(Token值不能帶空格)  
  2. -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken  
  3. {  
  4.   
  5.     NSLog(@"%@",deviceToken);  
  6.   
  7. }  
  8. ///Token值獲取失敗的時(shí)候走的是這個(gè)方法  
  9. -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error  
  10. {  
  11.   
  12.     NSLog(@"%@",error);  
  13. }  
  14. ///應(yīng)用程序處在打開(kāi)狀態(tài),且服務(wù)器有推送消息過(guò)來(lái)時(shí),以及通過(guò)推送打開(kāi)應(yīng)用程序,走的是這個(gè)方法  
  15. -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  
  16. {  
  17.     for (id key in userInfo) {  
  18.         NSLog(@"%@:%@",key, [userInfo objectForKey:key]);  
  19.     }  
  20.     ///Icon推送數(shù)量設(shè)為0  
  21. //    application.applicationIconBadgeNumber=0;  
  22. }  

應(yīng)用程序不處在后臺(tái),且通過(guò)推送通知打開(kāi)的時(shí)候,如果需要推送下來(lái)相關(guān)的信息可以在

 

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  

方法中加入

 

 

  1. ///應(yīng)用程序不處在后臺(tái),并且是通過(guò)推送打開(kāi)應(yīng)用的時(shí)候  
  2. if (launchOptions) {  
  3.     ///獲取到推送相關(guān)的信息  
  4.     NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];  
  5. }  

 

 


 

服務(wù)端PHP推送代碼

 

  1. <?php  
  2.     $deviceToken= 'ba6d5106503c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6'; //沒(méi)有空格  
  3.     $body = array("aps" => array("alert" => 'helloHui',"badge" => 2,"sound"=>'default'));  //推送方式,包含內(nèi)容和聲音  
  4.     $ctx = stream_context_create();  
  5.     //如果在Windows的服務(wù)器上,尋找pem路徑會(huì)有問(wèn)題,路徑修改成這樣的方法:  
  6.     //$pem = dirname(__FILE__) . '/' . 'apns-dev.pem';  
  7.     //linux 的服務(wù)器直接寫(xiě)pem的路徑即可  
  8.     stream_context_set_option($ctx,"ssl","local_cert","26ck.pem");  
  9.     $pass = "123123";  
  10.     stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
  11.     //此處有兩個(gè)服務(wù)器需要選擇,如果是開(kāi)發(fā)測(cè)試用,選擇第二名sandbox的服務(wù)器并使用Dev的pem證書(shū),如果是正是發(fā)布,使用Product的pem并選用正式的服務(wù)器  
  12. //    $fp = stream_socket_client("ssl://gateway.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  13.     $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  14.     if (!$fp) {  
  15.         echo "Failed to connect $err $errstrn";  
  16.         return;  
  17.     }  
  18.     print "Connection OK/n";  
  19.     $payload = json_encode($body);  
  20.     $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
  21.     echo "sending message :" . $payload ."/n";  
  22.     fwrite($fp, $msg);  
  23.     fclose($fp);?>  
  1. <pre></pre>  
  2. <pre></pre>  
  3. <pre></pre>  
  4. <pre></pre>  
  5. <pre></pre>  
  6. <pre></pre>  
  7.       
  8.         <div style="padding-top:20px">           
  9.             <p style="font-size:12px;">版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。</p>  
  10.         </div>  
 
 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 井冈山市| 扎赉特旗| 道孚县| 绵竹市| 巧家县| 西乌| 株洲县| 浮山县| 定襄县| 台中市| 株洲县| 东乌| 彭泽县| 吉木萨尔县| 丰原市| 额敏县| 奉贤区| 冀州市| 长垣县| 禄丰县| 东乡| 高陵县| 瓮安县| 泾川县| 定兴县| 怀安县| 富蕴县| 张掖市| 建水县| 德令哈市| 南昌县| 陵川县| 梓潼县| 辛集市| 大渡口区| 上蔡县| 盐源县| 广宁县| 礼泉县| 金堂县| 兰西县|