IOS信息推送demo

  • 2017-11-9 14:45
  • PHP二次开发
  • 阅读(170)
  • 评论(1
  • 来源:互联网
  • 摘要: 将之前公钥和私钥合成的pem文件复制到文件夹 - 创建一个PHP文件simplepush.php,其代码主要为:?phpset_time_limit(0);// 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)$deviceToken = 'fa1157487 ...
    将之前公钥和私钥合成的pem文件复制到文件夹 -> 创建一个PHP文件simplepush.php,其代码主要为:
    <?php
    set_time_limit(0);
    // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
    $deviceToken = 'fa1157487e3b4d37e6732d47183b4cdeb775e965950ff9201a9ba8dd6443411f';
    // Put your private key's passphrase here:
    $passphrase = '123456';
    // Put your alert message here:
    $message = 'My first push test!';
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'allow_self_signed', true);
    stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'aps_development_push.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    // Open a connection to the APNS server
    //这个为正是的发布地址
    // $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    //这个是沙盒测试地址,发布到appstore后记得修改哦
    $fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);
    echo 'Connected to APNS' . PHP_EOL;
    // Create the payload body
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
    );
    // Encode the payload as JSON
    $payload = json_encode($body);
    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));
    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
    else
        echo 'Message successfully delivered' . PHP_EOL;
    // Close the connection to the server
    fclose($fp);


    PHP技术交流QQ群:422137578

    除非注明,文章均为 PHP二次开发 原创,转载请注明本文地址:http://www.php2.cc/article-2668-1.html

    相关阅读

    发表评论

    最新评论

    引用 比优建站 PHP程序开发个人接单实惠   2017-11-16 10:03
    好样的试试就知道了

    查看全部评论(1)

    用户名:  *

    邮 箱:  *

    网 址: 注意加上“http://”哦!