【问题标题】:single push receives, but push to multiple device, don't receive单推接收,但推送到多个设备,不接收
【发布时间】:2015-08-27 01:14:08
【问题描述】:

我的数据库中注册了 16 台设备。当我推送到单个设备时,它发送和接收成功,但是当我迭代到我的所有设备时,它发送成功,但从未接收到。这是我的 php 代码:

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_prod.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

    $pid = 0;
    $msg = '';

   // this where it goes if we try to send single device, we define the device token to send
    if ($device_token != '') 
    {           
        $token = $device_token;

        $payload = json_encode($body);
        $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack("n",strlen($payload)) . $payload;

        if(!$fp)
        {
            $this->api_model->update_ios_push($device_token, $message, 'failed', $amg_id);
        }
        else
        {
            $this->api_model->update_ios_push($device_token, $message, 'delivered', $amg_id);
        }   

        fwrite($fp, $msg);      
    }

    //this if we want to send to all devices in db.
    else 
    {
        foreach ($devices as $device) 
        {
            $token = $device['devicetoken'];

            $payload = json_encode($body);
            $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack("n",strlen($payload)) . $payload;

            if(!$fp)
            {
                $this->api_model->update_ios_push($device['pid'], $message, 'failed', $amg_id);
            }
            else
            {
                $this->api_model->update_ios_push($device['pid'], $message, 'delivered', $amg_id);
            }
            $pid++;

            fwrite($fp, $msg);
        }       
    }
    fclose($fp);  

它发送成功,但从未在设备上接收。以前的项目还可以,但现在这段代码不再工作了。

【问题讨论】:

  • 仔细检查你$device['devicetoken']可能与$device_token不一样
  • 正确,您可以看到输出,它返回正确的令牌正在发送。

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


【解决方案1】:

您将不得不稍微更改您的代码;

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_prod.pem'); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', 'password');


    $pid = 0;
    $msg = '';

   // this where it goes if we try to send single device, we define the device token to send
    if ($device_token != '') 
    {
        $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);           

        $token = $device_token;

        $payload = json_encode($body);
        $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack("n",strlen($payload)) . $payload;

       $result = fwrite($fp, $msg);

       if(!$result)
        {
            $this->api_model->update_ios_push($device_token, $message, 'failed', $amg_id);
        }
        else
        {
            $this->api_model->update_ios_push($device_token, $message, 'delivered', $amg_id);
        }
        fclose($fp);         
    }

    //this if we want to send to all devices in db.
    else 
    {
        // set time limit to zero in order to avoid timeout
        set_time_limit(0);

        foreach ($devices as $device) 
        {
           // wait for some time
           sleep(1);

           $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

            $token = $device['devicetoken'];

            $payload = json_encode($body);
            $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $token)) . pack("n",strlen($payload)) . $payload;

            $pid++;

            $result = fwrite($fp, $msg);
            if(!$fp)
            {
                $this->api_model->update_ios_push($device['pid'], $message, 'failed', $amg_id);
            }
            else
            {
                $this->api_model->update_ios_push($device['pid'], $message, 'delivered', $amg_id);
            }

            //Closing connection every time
            fclose($fp);
        }

        // set time limit back to a normal value
        set_time_limit(30);       
    }

参考:http://www.kubilayerdogan.net/sample-apple-push-notification-php-script/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多