【问题标题】:How to send push notifications to swift app using PHP [Swift,Php]如何使用 PHP [Swift,Php] 向 swift 应用程序发送推送通知
【发布时间】:2019-01-12 16:52:22
【问题描述】:

我正在尝试使用我在 Internet 上找到的这个 php 脚本向 iOS 应用程序发送通知。 我对 $passphrase 有什么价值,在哪里可以找到它? 的值相同:

  • stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
  • stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase);
类推送通知 { 私人静态 $passphrase = 'joashp'; 公共函数 __construct() { exit('不允许初始化函数'); } 公共静态函数 iOS($data, $devicetoken) { $设备令牌 = $设备令牌; $ctx = stream_context_create(); // ck.pem 是你的证书文件 stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); stream_context_set_option($ctx, 'ssl', 'passphrase', self::$passphrase); // 打开与 APNS 服务器的连接 $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 如果 (!$fp) exit("连接失败:$err $errstr" . PHP_EOL); // 创建负载体 $body['aps'] = 数组( '警报' => 数组( 'title' => $data['mtitle'], 'body' => $data['mdesc'], ), '声音' => '默认' ); // 将有效负载编码为 JSON $payload = json_encode($body); // 构建二进制通知 $msg = chr(0) 。包('n',32)。包('H*',$deviceToken)。包('n',strlen($payload))。 $有效载荷; // 发送到服务器 $result = fwrite($fp, $msg, strlen($msg)); // 关闭与服务器的连接 fclose($fp); 如果(!$结果) 返回 '​​消息未送达' 。 PHP_EOL; 别的 返回 '​​消息已成功发送' 。 PHP_EOL; } // 卷曲 私有函数 useCurl(&$model, $url, $headers, $fields = null) { // 打开连接 $ch = curl_init(); 如果($url){ // 设置 url,POST 变量的数量,POST 数据 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 暂时禁用 SSL 证书支持 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 如果($字段){ curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); } // 执行发布 $result = curl_exec($ch); if ($result === FALSE) { die('Curl 失败:' . curl_error($ch)); } // 关闭连接 curl_close($ch); 返回$结果; } } } $msg_payload = 数组( 'mtitle' => '测试推送通知标题', 'mdesc' => '测试推送通知正文', ); $deviceToken = 'FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660'; PushNotifications::iOS($msg_payload, $deviceToken);

【问题讨论】:

  • 函数iOS 未声明为静态的。你应该实例化这个类
  • 谢谢,然后好吗?

标签: php swift


【解决方案1】:

$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

【讨论】:

    猜你喜欢
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多