iPhone 搭建PHP版Push服务器 实例操作
创始人
2024-08-02 19:10:29
0

iPhone 搭建PHPPush服务器 实例操作是本文介绍的内容。在应用里加入 Push 功能对于用户及时获取信息是非常有帮助的,以前介绍过 iPhone 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:
  6. (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];  
  7.  }  
  8.  - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {  
  9.      //NSLog(@"devToken=%@",deviceToken);  
  10.      [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"devToken=%@",deviceToken] cancleButtonTitle:@"Ok" otherButtonTitle:@""];  
  11.  }  
  12.  - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {  
  13.     NSLog(@"Error in registration. Error: %@", err);  
  14.      [self alertNotice:@"" withMSG:[NSString stringWithFormat:@"Error in registration. Error: %@", err]
  15.  cancleButtonTitle:@"Ok" otherButtonTitle:@""];  
  16.  }  
  17. . -(void)alertNotice:(NSString *)title withMSG:(NSString *)msg cancleButtonTitle:(NSString *)cancleTitle 
  18. otherButtonTitle:(NSString *)otherTitle{  
  19.      UIAlertView *alert;  
  20.     if([otherTitle isEqualToString:@""])  
  21.          alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle otherButtonTitles:nil,nil];  
  22.      else  
  23.          alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:cancleTitle 
  24. otherButtonTitles:otherTitle,nil];  
  25.      [alert show];  
  26.      [alert release];  
  27.  } 

b.在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 方法中增加

  1. [self alertNotice:@"" withMSG:@"Initiating Remote Noticationss Are Active" cancleButtonTitle:@"Ok" otherButtonTitle:@""];  
  2. [[UIApplication sharedApplication] registerForRemoteNotificationTypes:  
  3.  
  4.        (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)]; 

14.项目设置

a.Targets > $APP_NAME > context menu > Properties > Identifier

修改

  1. identifier  
  2.  为  
  3. App ID  
  4. b.Targets > $APP_NAME > context menu > Build > Code Signing > Code Signing Identifier > Any iPhone OS Device 

指定 iPhone Developer 为开发用机,编译并运行后会在iPhone上显示设备令牌

php Push Notification sender代码如下:

  1. deviceToken = "设备令牌";   
  2. $body = array("aps" => array("alert" => 'message', "badge" => 1, "sound" => 'received5.caf'));  
  3. $ctx = stream_context_create();  
  4. stream_context_set_option($ctx, "ssl", "local_cert", "ck.pem");   
  5. $fp = stream_socket_client("ssl://gateway.sandbox.push.apple.com:2195", $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
  6. if (!$fp) {  
  7.     print "Failed to connect $err $errstrn";  
  8.     return;  
  9. }  
  10. print "Connection OK\n";  
  11. payload = json_encode($body);  
  12. $msg = chr(0) . pack("n",32) . pack("H*", $deviceToken) . pack("n",strlen($payload)) . $payload;  
  13. rint "sending message :" . $payload . "\n";  
  14. fwrite($fp, $msg);  
  15. fclose($fp);  
  16. ?> 

小结:iPhone 搭建PHPPush服务器 实例操作的内容介绍完了,希望本文对你有帮助。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...