标签:des style blog http io ar color os 使用
iOS的推送可以用下图简单的概括:
(1). 登录 iPhone Developer Connection Portal (链接) 然后点击 App IDs
(2). 创建一个 Apple ID ,如:com.chenjungang.apnstest 注意:通配符 ID 不能用于推送通知服务。
(3). 点击 Apple ID 旁的“Configure”,根据“向导” 的步骤生成一个签名上传,然后下载生成的许可证。
(4). 双击 .cer 文件将你的 aps_development.cer 导入 Keychain 中。
(5). 在 Mac 上打开“钥匙串访问”,然后在“登录”中选择 "密钥"分类,找到我们创建的证书,然后右击“Apple Development IOS Push Services: com.tadpole.TestAPNs” > 导出 “Apple Development IOS Push Services: com.tadpole.TestAPNs”。保存为 cert.p12 文件。
(6). 通过终端命令将这个 cert.p12 文件转换为 PEM 格式,打开终端, cd 进入证书所在目录,执行如下命令:(Java 直接给 .p12 文件,Php 给 pem 文件)
openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
执行完上面命令会在当前目录下生成一个名为apple_push_notification_production.pem文件,这个文件就是我们需要得到php连接APNS 的文件,将apple_push_notification_production.pem和push.php放入同一目录上传到服务器,push.php的代码如下:
1 <?php 2 $deviceToken = ‘ad853c20 3a9b874e 00d0cde2 44a73752 999bb250 9fa36129 0525d5d0 a6ba3e4c‘; 3 4 // Put your private key‘s passphrase here: 5 $passphrase = ‘123456‘; 6 7 // Put your alert message here: 8 $message = ‘This is some push message!‘; 9 $ctx = stream_context_create(); 10 stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘apple_push_notification_production.pem‘); 11 stream_context_set_option($ctx, ‘ssl‘, ‘passphrase‘, $passphrase); 12 13 // Open a connection to the APNS server 14 //这个为正式的发布地址 15 //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx); 16 //这个是沙盒测试地址,发布到appstore后记得修改哦 17 $fp = stream_socket_client( 18 ‘ssl://gateway.sandbox.push.apple.com:2195‘, $err, 19 $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 20 if (!$fp) 21 exit("Failed to connect: $err $errstr" . PHP_EOL); 22 echo ‘Connected to APNS‘ . PHP_EOL; 23 24 // Create the payload body 25 $body[‘aps‘] = array( 26 ‘alert‘ => $message, 27 ‘sound‘ => ‘default‘ 28 ); 29 30 // Encode the payload as JSON 31 $payload = json_encode($body); 32 33 // Build the binary notification 34 $msg = chr(0) . pack(‘n‘, 32) . pack(‘H*‘, $deviceToken) . pack(‘n‘, strlen($payload)) . $payload; 35 36 // Send it to the server 37 $result = fwrite($fp, $msg, strlen($msg)); 38 39 if (!$result) 40 echo ‘Message not delivered‘ . PHP_EOL; 41 else 42 echo ‘Message successfully delivered‘ . PHP_EOL; 43 44 // Close the connection to the server 45 fclose($fp); 46 47 ?>
1 #define IS_IOS8_AND_UP ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) 2 3 -(void)settingPushMessage:(UIApplication*)application 4 { 5 6 if (IS_IOS8_AND_UP) { 7 [application registerForRemoteNotifications]; 8 UIUserNotificationType types = UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert; 9 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:types categories:nil]]; 10 }else{ 11 //消息推送支持的类型 12 UIRemoteNotificationType types = 13 (UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound); 14 //注册消息推送 15 [[UIApplication sharedApplication]registerForRemoteNotificationTypes:types]; 16 } 17 18 } 19 20 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 21 { 22 //获取token 23 NSString* token = [NSString stringWithFormat:@"%@",deviceToken]; 24 token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""]; 25 token = [token stringByReplacingOccurrencesOfString:@">" withString:@""]; 26 [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"APVNToken"]; 27 28 NSLog(@"token====%@",token); 29 } 30 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 31 { 32 //这里是获取token失败 33 NSString *error_str = [NSString stringWithFormat: @"%@", error]; 34 NSLog(@"Failed to get token, error:%@", error_str.description); 35 } 36 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 37 { 38 //在此处理接收到的消息。 39 NSLog(@"Receive remote notification : %@",userInfo); 40 }
1 { 2 aps = { 3 alert = "hello, everyone"; 4 badge = 2; 5 sound = default; 6 }; 7 }
1 { 2 action = { 3 type = 4; 4 }; 5 aps = { 6 alert = "hello, everyone"; 7 badge = 4; 8 }; 9 }
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/chenjungang/p/4162350.html