【问题标题】:Persistent connection via PHP to APNS通过 PHP 到 APNS 的持久连接
【发布时间】:2012-06-08 10:09:06
【问题描述】:

我知道 SO 上有很多帖子解决了这个问题,不幸的是我在 PHP 编程方面不是那么先进,而且我有一个其他地方没有回答的问题:

Apple 推送通知的许多教程通过 stream_socket_client() 创建连接。但他们中的大多数都缺少“STREAM_CLIENT_PERSISTENT”标志。这个标志会使连接真正持久吗?如果有,什么时候关闭?文档说它也会在页面重新加载时保持连接。这取决于会话吗?

没有这个标志的版本正在运行,但我担心一旦我输入生产证书等,APNS 会阻止我(描述为here)。 提前致谢。

【问题讨论】:

    标签: php ios push-notification apple-push-notifications


    【解决方案1】:

    根据Predefined Constants 上的 PHP 文档,使用带有 APNS 连接的 STREAM_CLIENT_PERSISTENT应该在页面加载之间保持连接处于活动状态。这是 APNS 连接的一项要求,因为它限制您,因为它会将在发送有效负载后的任何断开连接视为潜在的拒绝服务攻击。

    如果您在客户端连接之外有任何问题,您可能想尝试以下方法,因为这是迄今为止我见过的在 PHP 中处理 APNS 连接的最佳方式。这使用来自PHPXMLRPC 的客户端,因此您必须下载该软件包。

    <?php
    
    include '../vendors/xmlrpc.inc';
    
    $hostName = 'localhost'; # Your services endpoint here.
    $rpcPath = '';
    $port = 7077;
    
    if($_GET['action'] == 'provisioning')
    {
        $echoString = new xmlrpcmsg(
            'provision',
            array(
                php_xmlrpc_encode('appid'),
                php_xmlrpc_encode('/path/to/certificate.pem'),
                php_xmlrpc_encode('sandbox'),
                php_xmlrpc_encode(100)
            )
        );
        $continue = TRUE;
    }
    
    if($_GET['action'] == 'notify')
    {
        $echoString = new xmlrpcmsg(
            'notify',
            array(
                php_xmlrpc_encode('paparazzme'),
                php_xmlrpc_encode(array('6bcda...', '7c008...')),
                php_xmlrpc_encode(array(array("aps" => array("alert" => "Hello User 1" )), array("aps" => array("alert" => "Hello User 2" ))))
            )
        );
        $continue = TRUE;
    }
    
    if($continue == true)
    {
        # Create a client handle and send request
        $client = new xmlrpc_client($rpcPath, $hostName, $port);
    
        # A little verbose debug
        $client->setDebug(2);
    
        # The response
        $response = &$client->send($echoString);
    
        # Check if response is good
        if (! $response->faultCode())
            print "\nReturned string is: " . php_xmlrpc_decode($response->value()) . "\n";
        else
            print "An error occurred: \nCode: " . $response->faultCode() . " Reason: '" . htmlspecialchars($response->faultString()) . "'\n";
    }
    
    ?>
    

    来源:How to get started with APNS for iPhone or iTouch

    我想花点时间指出,我没有测试任何代码,我现在没有 iPhone 应用程序来测试它,所以我可以告诉你这是否真的有效。

    如果对您可行,我建议您改用 Uban Airship,因为他们确实每月向每个客户提供 250,000 次免费推送,并从那里为您处理与 APN 服务器的连接使用他们的APIs 与您的客户交谈。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2017-09-02
      • 1970-01-01
      相关资源
      最近更新 更多