别的不说,现在AppDelegate.m中添加以下代码块
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 将这行代码插入 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound]; }
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"deviceToken = %@", deviceToken); }
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"Error in registration. Error: %@", error); }
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSDictionary *dic = [userInfo objectForKey:@"aps"]; NSLog(@"收到的推送消息%@", [dic objectForKey:@"alert"]); if (nil != [dic objectForKey:@"alert"]) { UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"推送通知" message:[dic objectForKey:@"alert"] delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新状态", nil]; [alterView show]; [alterView release]; } }
将打印出来的deviceToken 去掉空格和<>符号,生成一个许可证号留着
======================================================
生成测试证书:
1、进入Provisioning Portal, 下载Certificates在development下的证书
2、 找到需要测试的app id,然后enable它在development下的Apple Push Notification service: Development Push SSL Certificate。需要输入1)中的签名证书才可以生成一个aps_developer_identity.cer.
3、双击aps_developer_identity.cer,会打开系统的key chain. 在My certificates下找到Apple Development Push Services。需要为certificate和它之下的private key (点前面的箭头,会出现一个钥匙) 各自export出一个.p12文件。(会出现设置密码过程)
4、将上面的2个.p12文件转成.pem格式:
openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
openssl pkcs12 -nocerts -out key.pem -in key.p12
5、如果需要对 key不进行加密:
openssl rsa -in key.pem -out key.unencrypted.pem
6、合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
cat cert.pem key.unencrypted.pem > ck.pem
================================================================
用php写服务器 send.php
#!/usr/bin/php <?php $deviceToken = ‘3cec682c9b773f2d82e337a98c35483aaa4f4c74e3933f9cb57e9d4c2f17f52d‘; // masked for security reason // Passphrase for the private key (ck.pem file) $pass = ‘‘; // Get the parameters from http get or from command line $message = $_GET[‘message‘] or $message = $argv[1] or $message = ‘Message received from javacom‘; $badge = (int)$_GET[‘badge‘] or $badge = (int)$argv[2]; $sound = $_GET[‘sound‘] or $sound = $argv[3]; $message = ‘Hello qwe‘; $badge = 1; $sound = ‘cowbell.wav‘; // Construct the notification payload $body = array(); $body[‘aps‘] = array(‘alert‘ => $message); if ($badge) $body[‘aps‘][‘badge‘] = $badge; if ($sound) $body[‘aps‘][‘sound‘] = $sound; /* End of Configurable Items */ $ctx = stream_context_create(); stream_context_set_option($ctx, ‘ssl‘, ‘local_cert‘, ‘ck.pem‘); // assume the private key passphase was removed. // 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) { print "Failed to connect $err $errstrn"; return; } else { print "Connection OK\n"; } $payload = json_encode($body); $msg = chr(0) . pack("n",32) . pack(‘H*‘, str_replace(‘ ‘, ‘‘, $deviceToken)) . pack("n",strlen($payload)) . $payload; print "sending message :" . $payload . "\n"; fwrite($fp, $msg); fclose($fp); ?>
把php文件与ck.pem放到同一文件夹下。
在命令行中,php send.php 即可测试