博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为 iPhone 应用搭建 php 版 push 服务器的流程
阅读量:4108 次
发布时间:2019-05-25

本文共 4724 字,大约阅读时间需要 15 分钟。

为 iPhone 应用搭建 php 版 push 服务器的流程

转自
http://www.cocoachina.com/macdev/network/2010/0826/2056.html

在应用里加入 Push 功能对于用户及时获取信息是非常有帮助的,以前介绍过 iPhone 的 Push (推送通知)功能原理浅析,里面提到要为自己的 App 添加推送功能,开发者先要搭建一个推送服务

  在应用里加入 Push 功能对于用户及时获取信息是非常有帮助的,以前介绍过 ,里面提到要为自己的 App 添加推送功能,开发者先要搭建一个推送服务器。下面就介绍一个为 iPhone 应用搭建 php 版 push 服务器的流程。

0.在Mac OS X机器上安装好XCode, 连接一台正常的iPhone, 保持平和的心态

APP 开发基础设置
1.在iPhone Provisioning Portal中建立好APP ID和Device.
2. 在Keychain Access.app中生成证书请求CertificateSigningRequest.certSigningRequest(菜单 > Keychain Access > Certificate Assistant > Request a Certificate From a Certificate Authority...).
3.在iPhone Provisioning Portal > Certificates中请求一个证书(点击Request Certificate,上传CertificateSigningRequest.certSigningRequest).
4.请求完成后,将证书文件(developer_identity.cer)下载,双击导入到Key Chain中.
5.在iPhone Provisioning Portal > Provisioning 中,新建一个Profile, 选择指定的APP ID和 Devices后生成.
6.将刚刚生成的Profile下载为*_profile.mobileprovision, 双击该文件, 将profile加载到iPhone中.
Push Notification service设置
7.在iPhone Provisioning Portal > App IDs,选择需要Push服务的App ID, 进入Configure.
8.确认 Enable for Apple Push Notification service ,配置 Development Push SSL Certificate, 上传第2步生成的证书请求.
9.下载生成的aps_developer_identity.cer, 完成Push服务配置.
10.双击aps_developer_identity.cer,保存到Key Chain.
生成php Push Notification sender需要的证书文件
11.在Keychain Access.app里选定这个新证书(Apple Development Push Services*),导出到桌面,保存为Certificates.p12.
12.运行如下命令:
   1.     openssl pkcs12 -clcerts -nokeys -out cert.pem -in Certificates.p12
   2.     openssl pkcs12 -nocerts -out key.pem -in Certificates.p12
   3.     openssl rsa -in key.pem -out key.unencrypted.pem
   4.     cat cert.pem key.unencrypted.pem > ck.pem
获得php Push Notification sender所需的设备令牌:
13.新建一个View-based Application项目,在$PROJECT_NAMEAppDelegate.m中:
a.粘贴如下代码:
   1. - (void)applicationDidFinishLaunching:(UIApplication *)app {
   2.     // other setup tasks here….
   3.     [window addSubview:viewController.view];
   4.     [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
   5.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
   6. }
   7. - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
   8.     //NSLog(@"devToken=%@",deviceToken);
   9.     [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  10. }
  11. - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
  12.     NSLog(@"Error in registration. Error: %@", err);
  13.     [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err] cancleButtonTitle:@"Ok" otherButtonTitle:@""];
  14. }
  15. -(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle otherButtonTitle:(NSString *)otherTitle{
  16.     UIAlertView *alert;
  17.     if([otherTitle isEqualToString:@""])
  18.         alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];
  19.     else
  20.         alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:otherTitle,nil];
  21.     [alert show];
  22.     [alert release];
  23. }
b.在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 方法中增加
   1.     [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];
   2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
14.项目设置
a.Targets > $APP_NAME > context menu > Properties > Identifier
    修改 identifier 为App ID
b.Targets > $APP_NAME > context menu > Build > Code Signing > Code Signing Identifier > Any iPhone OS Device
    指定 iPhone Developer 为开发用机
15.编译并运行后会在iPhone上显示设备令牌
16.php Push Notification sender代码如下:
   1. <?php
   2. $deviceToken = "设备令牌";
   3.  
   4. $body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'received5.caf'));
   5.  
   6. $ctx = stream_context_create();
   7. stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");
   8.  
   9. $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  10. if (!$fp) {
  11.     print "Failed to connect $err $errstrn";
  12.     return;
  13. }
  14. print "Connection OK\n";
  15. $payload = json_encode($body);
  16. $msg = chr(0) . pack("n",32) . pack("H*", $deviceToken) . pack("n",strlen($payload)) . $payload;
  17. print "sending message :" . $payload . "\n";
  18. fwrite($fp, $msg);
  19. fclose($fp);
  20. ?>

转载地址:http://wzvsi.baihongyu.com/

你可能感兴趣的文章
cocos2d-x学习笔记12:如何将win32移植到iOS
查看>>
cocos2d-x学习笔记13:动作4:其他动作
查看>>
cocos2d-x学习笔记14:粒子系统1:简介&工具使用
查看>>
cocos2d-x学习笔记15:cocos2d-x教程资源总结
查看>>
cocos2d-x学习笔记16:记录存储1:CCUserDefault
查看>>
cocos2d-x学习笔记17:记录存储2:SQLite基本使用
查看>>
cocos2d-x学习笔记18:内存管理01:概述
查看>>
方便地创建Mac App的iconset(附转换脚本)
查看>>
从零开始搭建Cydia软件源+制作deb安装包
查看>>
sqlite3 转义
查看>>
IOS设计UI工具大全
查看>>
ios 监听通讯录更新
查看>>
如何测试洗牌程序
查看>>
NSString与ASCII相互转化
查看>>
2012移动开发教程盘点:最棒的国外游戏开发站
查看>>
iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
查看>>
subprocess pre-removal script returned error exit status... 处理办法
查看>>
使用genstrings和NSLocalizedString实现App文本的本地化
查看>>
如何在iOS中使用ZXing库(ZXing是一个开源的条码生成和扫描库,开源协议为Apache2.0。它持众多条码格式和语言,比如Java、 C++、 C#、 Objective-C以及Act )
查看>>
如何正确地重命名Xcode项目
查看>>