douyan8070 2014-01-02 13:52
浏览 21

APNS多个registerId

My App is using APNS push. Now I read that you can leave the connection open while sending multiple messages. So reason enough for me to change some scripts.

My APNS class has the following methods(and many more, but these are the most important for now):

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;
}

Now I'm testing the script with some user typed data: (i've removed last 6 characters of registerId, for safety purposes). Using this array, both devices are receiving the Push Message.

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

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

)

Using a query to retrieve other (more real live) data provided with the following array:

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
        )

)

As you can see that are 2 the same registerId's and 2 new registerId's. However, none of the 4 devices is receiving a push notification.

I'm, kind of stuck or lost... Why is the second array failing?

  • 写回答

1条回答 默认 最新

  • dongqindu8110 2014-01-03 14:51
    关注

    The most likely explanation is that the first device token you send the message to (165ad6daa6e6e2c470d1c2777901e731f34fbe419f7d93cda383665c39) is invalid. When Apple gets this token, they close the connection. Your code sends the following 3 notifications before realizing the connection was dropped, and that's why none of them are sent.

    As for not getting any error response from Apple, you are sending the notifications in the old format (the one in which the first byte is 0). In order to be able to get error responses, you must use one of the newer formats (those that begin with 1 or 2 and contain a message ID).

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分