iPhone应用中APNS推送通知流程代码实现案例
来源:互联网 发布:linux用户切换命令 编辑:程序博客网 时间:2024/05/22 00:24
iPhone应用中APNS推送通知流程代码实现案例是本文要介绍的内容,主要是如何来实现APNS的推送通知,具体内容来看本文详细代码。
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.
1. - (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
2. [[UIApplication sharedApplication] registerForRemoteNotificationTypesUIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
3. // other codes here.
4. return YES;
5. }
6.
7. - (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken {
8. NSLog(@"deviceToken: %@", deviceToken);
9. }
10.
11. - (void)applicationUIApplication *)application didFailToRegisterForRemoteNotificationsWithErrorNSError *)error {
12. NSLog(@"Error in registration. Error: %@", error);
13. }
14.
15. - (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo
16. {
17.
18. NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey"aps"] objectForKey"alert"]);
19. if ([[userInfo objectForKey"aps"] objectForKey"alert"]!=NULL) {
20. UIAlertView* alert = [[UIAlertView alloc] initWithTitle"推送通知"
21. message:[[userInfo objectForKey"aps"] objectForKey"alert"]
22. delegate:self
23. cancelButtonTitle" 关闭"
24. otherButtonTitles" 更新状态",nil];
25. [alert show];
26. [alert release];
27. }
28. }
启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
1. deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
2、生成app在服务端需要的许可证
(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格式:
1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
2. openssl pkcs12 -nocerts -out key.pem -in key.p12
(5)如果需要对 key不进行加密:
1. openssl rsa -in key.pem -out key.unencrypted.pem
(6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
1. cat cert.pem key.unencrypted.pem > ck.pem
3、服务端push通知到ANPS. 在cocoachina找到了两种方法:
(1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
1. <?php
2. $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
3. $pass = '123456'; // Passphrase for the private key (ck.pem file)
4.
5. // Get the parameters from http get or from command line
6. $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
7. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
8. $sound = $_GET['sound'] or $sound = $argv[3];
9.
10. // Construct the notification payload
11. $body = array();
12. $body['aps'] = array('alert' => $message);
13. if ($badge)
14. $body['aps']['badge'] = $badge;
15. if ($sound)
16. $body['aps']['sound'] = $sound;
17.
18. /* End of Configurable Items */
19. $ctx = stream_context_create();
20. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
21. // assume the private key passphase was removed.
22. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
23.
24. // connect to apns
25. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
26. if (!$fp) {
27. print "Failed to connect $err $errstrn";
28. return;
29. }
30. else {
31. print "Connection OKn<br/>";
32. }
33.
34. // send message
35. $payload = json_encode($body);
36. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
37. print "Sending message :" . $payload . "n";
38. fwrite($fp, $msg);
39. fclose($fp);
40. ?>
请 求一次 http://127.0.0.1/apns /apns.php?message=A%20test%20message%20from%20localhost&badge=2& sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
1. Connection OK
2. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}
(2)pushMeBaby 驱动。将aps_developer_identity.cer导入到project里面,改名为apns.cer。
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken.
1. - (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
2. [[UIApplication sharedApplication] registerForRemoteNotificationTypesUIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
3. // other codes here.
4. return YES;
5. }
6.
7. - (void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)deviceToken {
8. NSLog(@"deviceToken: %@", deviceToken);
9. }
10.
11. - (void)applicationUIApplication *)application didFailToRegisterForRemoteNotificationsWithErrorNSError *)error {
12. NSLog(@"Error in registration. Error: %@", error);
13. }
14.
15. - (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo
16. {
17.
18. NSLog(@" 收到推送消息 : %@",[[userInfo objectForKey"aps"] objectForKey"alert"]);
19. if ([[userInfo objectForKey"aps"] objectForKey"alert"]!=NULL) {
20. UIAlertView* alert = [[UIAlertView alloc] initWithTitle"推送通知"
21. message:[[userInfo objectForKey"aps"] objectForKey"alert"]
22. delegate:self
23. cancelButtonTitle" 关闭"
24. otherButtonTitles" 更新状态",nil];
25. [alert show];
26. [alert release];
27. }
28. }
启动程序,将app注册到通知项后,在console里面找到打印的deviceToken:
1. deviceToken: <6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b>
2、生成app在服务端需要的许可证
(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格式:
1. openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12
2. openssl pkcs12 -nocerts -out key.pem -in key.p12
(5)如果需要对 key不进行加密:
1. openssl rsa -in key.pem -out key.unencrypted.pem
(6)然后就可以 合并两个.pem文件, 这个ck.pem就是服务端需要的证书了。
1. cat cert.pem key.unencrypted.pem > ck.pem
3、服务端push通知到ANPS. 在cocoachina找到了两种方法:
(1)php驱动。需要将ck.pem和php脚本放到server 上。全部的php代码是:
1. <?php
2. $deviceToken = '6974ac11 870e09fa 00e2238e 8cfafc7d 2052e342 182f5b57 fabca445 42b72e1b';
3. $pass = '123456'; // Passphrase for the private key (ck.pem file)
4.
5. // Get the parameters from http get or from command line
6. $message = $_GET['message'] or $message = $argv[1] or $message = 'A test message from worldcup';
7. $badge = (int)$_GET['badge'] or $badge = (int)$argv[2];
8. $sound = $_GET['sound'] or $sound = $argv[3];
9.
10. // Construct the notification payload
11. $body = array();
12. $body['aps'] = array('alert' => $message);
13. if ($badge)
14. $body['aps']['badge'] = $badge;
15. if ($sound)
16. $body['aps']['sound'] = $sound;
17.
18. /* End of Configurable Items */
19. $ctx = stream_context_create();
20. stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
21. // assume the private key passphase was removed.
22. stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
23.
24. // connect to apns
25. $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
26. if (!$fp) {
27. print "Failed to connect $err $errstrn";
28. return;
29. }
30. else {
31. print "Connection OKn<br/>";
32. }
33.
34. // send message
35. $payload = json_encode($body);
36. $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
37. print "Sending message :" . $payload . "n";
38. fwrite($fp, $msg);
39. fclose($fp);
40. ?>
请 求一次 http://127.0.0.1/apns /apns.php?message=A%20test%20message%20from%20localhost&badge=2& sound=received5.caf就 会向APNS进行一次推送。我的请求结果如下:
1. Connection OK
2. Sending message :{"aps":{"alert":"A test message from localhost","badge":2,"sound":"received5.caf"}}
(2)pushMeBaby 驱动。将aps_developer_identity.cer导入到project里面,改名为apns.cer。
- iPhone应用中APNS推送通知流程代码实现案例
- iPhone应用中APNS推送通知流程代码实现案例
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- APNS推送通知的流程
- iPhone开发推送通知流程实现
- iPhone开发推送通知流程实现
- APNS推送最新代码流程
- golang 实现 iOS http2 推送 Apns通知
- iPhone开发之 - 苹果推送通知服务(APNs)编程
- IOS---APNS,推送通知
- APPLE推送通知APNS
- APPLE推送通知APNS
- 用汇编的眼光看C++(开篇)
- OGNL表达式语言学习
- iPhone消息推送机制实现与探讨
- 溢出工具包 Metasploit使用教程
- 开源框架Core Plot的使用
- iPhone应用中APNS推送通知流程代码实现案例
- VC打开文件系统报错,IDE崩溃解决
- Common Lisp函数的基础知识
- 深入浅出REST
- Libyan Military Captures Gadhafi's Fugitive Son
- Android事件处理
- Core Plot SDK的用法
- apple push notification service apple与Python结合推送
- Unity3D 游戏引擎之游戏对象的访问绘制线与绘制面详解(十七)