php - APNS 多个 registerId

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

我的应用程序正在使用 APNS 推送。现在我读到您可以在发送多条消息时保持连接打开。所以有足够的理由让我改变一些脚本。

我的 APNS 类有以下方法(还有很多,但这些是目前最重要的):

public function setRegisterId($registerId){
    $this->registerId = $registerId;
}

public function setupConnection()
{
    $APNSdetails[1] = array('pemFile'=>'test.pem', 'passPhrase'=>'passPhrase');
    $APNSdetails[2] = array('pemFile'=>'mag.pem', 'passPhrase'=>'passPhrase');

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', '/as/'.$APNSdetails[$this->appId]['pemFile']);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $APNSdetails[$this->appId]['passPhrase']);

    // Open a connection to the APNS server
    $this->connection = stream_socket_client(
            'ssl://gateway.push.apple.com:2195', $err,
            $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$this->connection){
        return FALSE;
    }
    else{
        return TRUE;
    }
}

public function sendMessage()
{
    $RegisterIds = $this->registerId;
    foreach ($RegisterIds AS $key => $value){

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $RegisterIds[$key]['registerId']) . pack('n', strlen($this->payload)) . $this->payload;

        // Send it to the server
        $result = fwrite($this->connection, $msg, strlen($msg));

        $RegisterIds[$key]['payload'] = $this->payload;

        if (!$result){
            $RegisterIds[$key]['result'] = 'Message not delivered';
        }
        else{
            $RegisterIds[$key]['result'] = 'Message successfully delivered';
        }
    }

    $this->results = $RegisterIds;
}

现在我正在用一些用户输入的数据测试脚本:(为了安全起见,我已经删除了 registerId 的最后 6 个字符)。使用这个数组,两个设备都在接收推送消息。

Array
(
    [0] => Array
        (
            [registerId] => ddbb8b9b27beed70e56866d07a3da5f4cabf71ffab4606967a523e93eb
            [id] => 214
            [deviceOs] => apple
        )

    [1] => Array
        (
            [registerId] => c20f10e0b7f345988440d860bfb179f5b95a47d7eee714f5e0268c2c93
            [id] => 234
            [deviceOs] => apple
        )

)

使用查询检索以下数组提供的其他(更真实的实时)数据:

Array
(
    [0] => Array
        (
            [registerId] => 165ad6daa6e6e2c470d1c2777901e731f34fbe419f7d93cda383665c39
            [id] => 1111
            [deviceOS] => apple
        )

    [1] => Array
        (
            [registerId] => ddbb8b9b27beed70e56866d07a3da5f4cabf71ffab4606967a523e93eb
            [id] => 1328
            [deviceOS] => apple
        )

    [2] => Array
        (
            [registerId] => 7383d29d4bc210b32ea409e46a99ca522a3cf4a51e330f7d7457f7bc88
            [id] => 359
            [deviceOS] => apple
        )

    [3] => Array
        (
            [registerId] => c20f10e0b7f345988440d860bfb179f5b95a47d7eee714f5e0268c2c93
            [id] => 1148
            [deviceOS] => apple
        )

)

如您所见,有 2 个相同的 registerId 和 2 个新的 registerId。但是,这 4 台设备都没有收到推送通知。

我有点卡住了或迷路了……为什么第二个阵列失败了?

最佳答案

最可能的解释是您将消息发送到的第一个设备 token (165ad6daa6e6e2c470d1c2777901e731f34fbe419f7d93cda383665c39) 无效。当 Apple 获得此 token 时,他们会关闭连接。您的代码在意识到连接断开之前发送了以下 3 条通知,这就是为什么没有发送它们的原因。

至于没有收到 Apple 的任何错误响应,您正在以旧格式(第一个字节为 0 的格式)发送通知。为了能够获得错误响应,您必须使用一种较新的格式(以 12 开头并包含消息 ID 的格式)。

关于php - APNS 多个 registerId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20884507/

相关文章:

ios - 应用程序的生命周期 - 当应用程序终止并且推送工具包有效负载到来时

php - 如何一次替换多个WordPress帖子标题中的特定单词?

php - 使用准备好的语句在数据库中插入 datetime NOW()

PHP 回显数组的结果

ios - 从现有的 NSString 构建 unicode 字符串

ios - 无法在 NSManagedObjects 之间建立关系

php - PDO 和存储过程的无缓冲查询错误

ios - UIWebView+AF联网问题

ios - 使用内容可用发送带有Parse Server Cloud Code的推送通知不起作用

ios - 在 APNS 的推送通知中发送摄像头