添加本地推送
///本地添加
- -(void)addLocalPushNotification:(UIButton*)sender;
- {
-
-
- NSLog(@"%s",__FUNCTION__);
- UILocalNotification* localNotification=[[UILocalNotification alloc]init];
-
- if (localNotification) {
-
- NSDate* pushDate=[NSDate dateWithTimeIntervalSinceNow:20];
-
- localNotification.timeZone=[NSTimeZone defaultTimeZone];
-
- localNotification.fireDate=pushDate;
-
- localNotification.repeatInterval=kCFCalendarUnitDay;
-
- localNotification.soundName=UILocalNotificationDefaultSoundName;
-
- localNotification.alertBody=@"Hello world";
-
- localNotification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"];
-
-
- NSDictionary* infoDic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- localNotification.userInfo=infoDic;
-
-
- UIapplication* app=[UIApplication sharedApplication];
- BOOL status=YES;
- for (UILocalNotification* notification in app.scheduledLocalNotifications) {
- if ([notification.userInfo objectForKey:@"key"]) {
- status=NO;
- }
- }
-
- if (status) {
-
- [app scheduleLocalNotification:localNotification];
- }
-
-
-
- NSLog(@"%@",app.scheduledLocalNotifications);
- }
- }
取消本地推送
- -(void)removeLocalPushNotification:(UIButton*)sender
- {
- NSLog(@"%s",__FUNCTION__);
- UIApplication* app=[UIApplication sharedApplication];
-
- NSArray* localNotifications=[app scheduledLocalNotifications];
-
- if (localNotifications) {
-
- for (UILocalNotification* notification in localNotifications) {
-
- NSDictionary* dic=notification.userInfo;
-
- if (dic) {
- NSString* key=[dic objectForKey:@"key"];
- if ([key isEqualToString:@"name"]) {
-
- [app cancelLocalNotification:notification];
-
- break;
- }
- }
-
- }
- }
-
-
-
-
- }
遠(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文件
- openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem
- openssl pkcs12 -nocerts -out PushChatKey.pem -in Push.p12
- cat PushChatCert.pem PushChatKey.pem > newck.pem
代碼實(shí)現(xiàn)如下
注冊(cè)推送通知
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
- (UIRemoteNotificationTypeAlert|
- UIRemoteNotificationTypeBadge|
- UIRemoteNotificationTypeSound)];
在AppDelegate中加入以下幾個(gè)代理方法
- -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
- {
-
- NSLog(@"%@",deviceToken);
-
- }
- -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
- {
-
- NSLog(@"%@",error);
- }
- -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- for (id key in userInfo) {
- NSLog(@"%@:%@",key, [userInfo objectForKey:key]);
- }
-
- }
應(yīng)用程序不處在后臺(tái),且通過(guò)推送通知打開(kāi)的時(shí)候,如果需要推送下來(lái)相關(guān)的信息可以在
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中加入
- if (launchOptions) {
-
- NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
- }
服務(wù)端PHP推送代碼
- <?php
- $deviceToken= 'ba6d5106503c8e62e68b5df1b36c3b58ced1588c6dabe0fc9e6828961aeb12d6';
- $body = array("aps" => array("alert" => 'helloHui',"badge" => 2,"sound"=>'default'));
- $ctx = stream_context_create();
-
-
-
- stream_context_set_option($ctx,"ssl","local_cert","26ck.pem");
- $pass = "123123";
- stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
-
- $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
- if (!$fp) {
- echo "Failed to connect $err $errstrn";
- return;
- }
- print "Connection OK/n";
- $payload = json_encode($body);
- $msg = chr(0) . pack("n",32) . pack("H*", str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
- echo "sending message :" . $payload ."/n";
- fwrite($fp, $msg);
- fclose($fp);?>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
- <pre></pre>
-
- <div style="padding-top:20px">
- <p style="font-size:12px;">版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。</p>
- </div>