zoukankan      html  css  js  c++  java
  • PHP调用APNS服务发送提醒样例

    转自:https://blog.csdn.net/ligaofeng/article/details/40658643

     1.

    <?php
    /*
    官方文档https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html
    */
    $apnsHost = 'gateway.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'apns-production.pem';
    $aData['aps'] = array('alert' => '收到后请给李高峰个电话', 'badge' => 1, 'sound' => 'default');
    $payload = json_encode($aData);
    $streamContext = stream_context_create();
    $res = stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
     if (!$apns) {
        print "Failed to connect $err $errstrn";
        return false;
    }
    $deviceToken = 'd14e47t13d258f6d5a4d48547a6d1826f2a6b80fefd9d3119cadf0102648ed0';
    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
     
    $result = fwrite($apns, $apnsMessage);
    if ($result) {
        echo 'Message successfully delivered';
    } else {
        echo 'Message not delivered';
    }
    socket_close($apns);
    fclose($apns);
    ?>

     2.

    <?php
    // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
    $deviceToken = 'device token';
    // Put your alert message here:
    $message = 'My first push test!.';
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.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;
    var_dump($result);
    // Close the connection to the server
    fclose($fp);
    ?>
  • 相关阅读:
    C#中 @ 的用法
    ASP.NET页面间传值
    ASP.NET中常用的文件上传下载方法
    把图片转换为字符
    把图片转换为字符
    JavaScript 时间延迟
    Using WSDLs in UCM 11g like you did in 10g
    The Definitive Guide to Stellent Content Server Development
    解决RedHat AS5 RPM安装包依赖问题
    在64位Windows 7上安装Oracle UCM 10gR3
  • 原文地址:https://www.cnblogs.com/zinging/p/15632292.html
Copyright © 2011-2022 走看看