Home  >  Article  >  php教程  >  ios开发:用PHP实现IOS推送

ios开发:用PHP实现IOS推送

WBOY
WBOYOriginal
2016-06-21 08:49:411199browse

 ios推送消息是个非常有用的功能,许多应用程序都具备了这个功能,成为实时应用的数据流核心.那么我们怎么用php为ios做推送服务呢?下面本文章将为您进行详细讲解。

ios消息推送机制可以参考ios消息推送机制实现与探讨。

首先,需要一个pem的证书,该证书需要与开发时签名用的一致。 具体生成pem证书方法如下:

1. 登录 iPhone Developer Connection Portal(http://developer.apple.com/iphone/manage/overview/index.action ) 然后点击 App IDs
2. 创建一个 Apple ID 。通配符 ID 不能用于推送通知服务。如,  com.itotem.iphone
3. 点击Apple ID旁的“Configure”,根据“向导” 的步骤生成一个签名上传,然后下载生成的许可证。
4. 双击.cer文件将你的 aps_developer_identity.cer 导入Keychain中。
5. 在Mac上启动 Keychain助手,然后在login keychain中选择 Certificates分类。看到一个可扩展选项“Apple Development Push Services”
6. 扩展此选项然后右击“Apple Development Push Services” > Export “Apple Development Push Services ID123”。保存为 apns-dev-cert.p12 文件。
7. 扩展“Apple Development Push Services” 对“Private Key”做同样操作,保存为 apns-dev-key.p12 文件。
8. 通过终端命令将这些文件转换为PEM格式:openssl pkcs12 -clcerts -nokeys -out apns-dev-cert.pem -in apns-dev-cert.p12
openssl pkcs12 -nocerts -out apns-dev-key.pem -in apns-dev-key.p12
9. 最后,你需要将键和许可文件合成为apns-dev.pem文件,此文件在连接到APNS时需要使用:
cat apns-dev-cert.pem apns-dev-key-noenc.pem > apns-dev.pem

 

PHP代码如下:

 

  1. $deviceToken = $_POST['token']; //取得Token
  2. $body = array(“aps” => array(“alert” => ‘message’, “badge” => 2, “sound”=>’default’));  //推送方式,包含内容和声音
  3. $ctx = stream_context_create();
  4. //如果在Windows的服务器上,寻找pem路径会有问题,路径修改成这样的方法:
  5. //$pem = dirname(__FILE__) . ‘/’ . ‘apns-dev.pem’;
  6. //linux 的服务器直接写pem的路径即可
  7. stream_context_set_option($ctx, “ssl”, “local_cert”, “apns-dev.pem”);
  8. $pass = ”123123“;
  9. stream_context_set_option($ctx, ‘ssl’, ‘passphrase’, $pass);
  10. //此处有两个服务器需要选择,如果是开发测试用,选择第二名sandbox的服务器并使用Dev的pem证书,如果是正是发布,使用Product的pem并选用正式的服务器
  11. $fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  12. $fp = stream_socket_client(“ssl://gateway.sandbox.push.apple.com:2195″, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
  13. if (!$fp) {
  14. print “Failed to connect $err $errstrn”;
  15. return;
  16. }
  17. print “Connection OK\n”;
  18. $payload = json_encode($body);
  19. $msg = chr(0) . pack(“n”,32) . pack(“H*”, str_replace(‘ ‘, ”, $deviceToken)) . pack(“n”,strlen($payload)) . $payload;
  20. print “sending message :” . $payload . “\n”;
  21. fwrite($fp, $msg);
  22. fclose($fp);
  23. ?>

 

然后,

到这里证书已经准备完毕,接下来,我们在xcode中新建一个测试工程,注意设置工程的Bundle Identifier必须与上面建的APP ID 里的相同


在didFinishLaunchingWithOptions 中加入一下代码

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

[self.window makeKeyAndVisible];

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge UIRemoteNotificationTypeSound  UIRemoteNotificationTypeAlert];

    return YES;

}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {

    NSLog(@"regisger success:%@", pToken);

}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{

    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"推送通知" message:@"信息" delegate:selfcancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];

    [alert show];

    [alert release];

}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {

    NSLog(@"Regist fail%@",error); 

}

接下来我们访问http://localhost/push/push.php

ios设备就会接收到一条推送消息了

另外去除标记的方法为,在viewDidApper中加入

 

int badge = [UIApplication sharedApplication].applicationIconBadgeNumber;

    if(badge > 0)

    {

        badge--;

        [UIApplication sharedApplication].applicationIconBadgeNumber = badge;

    }



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn