繁体   English   中英

如何使用PHP [Swift,Php]将推送通知发送到swift应用

[英]How to send push notifications to swift app using PHP [Swift,Php]

我正在尝试使用在Internet上找到的此php脚本将通知发送到iOS应用。 我应该为$ passphrase赋予什么价值,在哪里可以找到它? 相同的值:

  • stream_context_set_option($ ctx,'ssl','local_cert','ck.pem');
  • stream_context_set_option($ ctx,'ssl','passphrase',self :: $ passphrase);
class PushNotifications {
    private static $passphrase = 'joashp';

    public function __construct() {
        exit('Init function is not allowed');
    }

    public static function iOS($data, $devicetoken) {

        $deviceToken = $devicetoken;
        $ctx = stream_context_create();
        // ck.pem is your certificate file
        stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
        stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
        // Open a connection to the APNS server
        $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);
        // Create the payload body
        $body['aps'] = array(
            'alert' => array(
                'title' => $data['mtitle'],
                'body' => $data['mdesc'],
            ),
            '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));

        // Close the connection to the server
        fclose($fp);
        if (!$result)
            return 'Message not delivered' . PHP_EOL;
        else
            return 'Message successfully delivered' . PHP_EOL;
    }

    // Curl
    private function useCurl(&$model, $url, $headers, $fields = null) {
        // Open connection
        $ch = curl_init();
        if ($url) {
            // Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // Disabling SSL Certificate support temporarly
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            if ($fields) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            }

            // Execute post
            $result = curl_exec($ch);
            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);

            return $result;
        }
    }

}
$msg_payload = array (
    'mtitle' => 'Test push notification title',
    'mdesc' => 'Test push notification body',
);

$deviceToken = 'FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660';
PushNotifications::iOS($msg_payload, $deviceToken);

$ passphrase是您的证书文件ck.pem文件的密码。 您应该向生成该pem文件的人询问密码。

在许多情况下,密码短语可以为空。 在这种情况下,您可能需要提供一个空字符串。

如果您不知道什么是该pem文件-那么您可以阅读一些指南,例如https://developers.connectycube.com/ios/how-to-create-apns-certificate

本指南告诉您如何生成可用于发送Apple推送通知的p12证书文件。 然后,您可以使用以下命令从该p12中获取一个pem文件: https : //gist.github.com/shahdhiren/9ca059ac0762f7ef0fcf71a79ed5b022

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM