【问题标题】:push notification in iosios中的推送通知
【发布时间】:2012-12-18 21:01:00
【问题描述】:

我是 php 新手,在 codeigniter 中编写了推送通知的代码,但我得到了这些错误。

这是我的模型..

function sendmessage($appid, $deviceid, $status, $message)
{

    $deviceToken = '0f744707bebcf74f9b7c25d48e3358945f6aa01da5ddb387462c7eaf61bbad78';

    $message = 'My first push notification!';

    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    //stream_context_set_option($ctx, 'ssl', 'passphrase', 'essar@123');

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

    if (!$fp){
        exit("Failed to connect: $err $errstr" . PHP_EOL);
              }
          else {
          print "Connection OK/n";
               }
    echo 'Connected to APNS' . PHP_EOL;

    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
        );
    $payload = json_encode($body);
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    $result = fwrite($fp, $msg, strlen($msg));

    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
    else
        echo 'Message successfully delivered' . PHP_EOL;
    fclose($fp);

    $data = array(
    'message' => $this->message . 'add',
    'appid' => $this->appid,
    'deviceid' => $this->deviceid,
    'status' => $status
            );
    $this->sendmessage($data);

错误信息:

消息:stream_socket_client():SSL 操作失败,代码为 1。OpenSSL 错误消息:错误:14094410:SSL 例程:SSL3_READ_BYTES:sslv3 警报握手失败消息:stream_socket_client():无法启用加密消息:stream_socket_client():无法连接到 ssl://gateway.sandbox.push.apple.com:2195(未知错误)

【问题讨论】:

  • 您是否阅读过此答案:stackoverflow.com/questions/7453015/… - 基本上,请确保您使用的 ck.pem 文件的路径正确。
  • 确保您的服务器能够通过 2195 端口访问沙盒服务器

标签: php ios push-notification


【解决方案1】:

一旦您为开发和分发创建了临时证书和推送通知。按照生成推送通知的步骤

为了使用您生成的证书,您需要创建一个 PEM 文件 来存储您的 Apple Push Notification Service SSL 证书和您的私钥。您可以从终端创建 PEM 文件。

导航到包含您之前生成的证书和密钥的目录并执行以下步骤。此处的文件名反映了作为本课一部分生成的证书的名称。您必须根据您提供的证书名称更新语法。

首先创建应用程序证书 PEM 文件。您可以通过双击 aps_developer_identity.cer 证书文件,然后打开 Keychain Assistant 并将证书导出为 ap12 文件,然后将其转换为 PEM 文件,方法与将 PushNotificationApp.p12 转换为 PEM 的方式相同文件。或者,您可以使用单个命令行将 aps_developer_identity.cer 证书文件直接转换为 PEM 文件。这里我们选择单命令行选项,如下:

openssl x509 -inform der -outform pem -in aps_developer_identity.cer -out PushNotificationAppCertificate.pem

现在创建应用程序密钥 PEM 文件,如下所示。您需要输入导入密码和 PEM 密码:

openssl pkcs12 -in PushNotificationApp.p12 -out PushNotificationAppKey.pem -nocerts

输入导入密码: MAC 验证正常 输入 PEM 密码: 验证 - 输入 PEM 密码:

现在连接两个文件:

cat PushNotificationAppCertificate.pem PushNotificationAppKey.pem > PushNotificationAppCertificateKey.pem

打开 Mac 终端并从包含您生成的证书的目录中执行以下行:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushNotificationAppCertificate.pem -key PushNotificationAppKey.pem

然后要求您输入您提交的密钥的密码:

Enter pass phrase for PushNotificationAppKey.pem:

如果一切正常,那么服务器应该会向您发送大量信息,如下所示:

CONNECTED(00000003)

depth=1 /C=US/O=Entrust, Inc./OU=www.entrust.net/rpa is incorporated by reference/OU=(c) 2009 Entrust, Inc./CN=Entrust Certification Authority - L1C
verify error:num=20:unable to get local issuer certificate verify return:0
...
Key-Arg : None

Start Time: 1326899631 Timeout : 300 (sec) Verify return code: 0 (ok)
At the end of this, you can enter some text and then select the return key. We entered the text "**Hello World**".

**Hello World

closed**

这完成了与服务器的通信并验证我们的证书是否有效。

【讨论】:

    【解决方案2】:

    我遇到了与您在此处提到的相同的问题。这花了一些时间和挠头才找到......

    我发现纠正握手错误的解决方案是下载entrust 证书,并使用以下代码将其包含在流上下文中:

    $entrustCert =  '<full path to cert>/entrust_2048_ca.cer';
    stream_context_set_option($ctx, 'ssl', 'cafile', entrustCert);
    


    沙盒 APN 服务似乎确实存在间歇性连接问题。我偶尔会收到如下错误返回:

    Warning: stream_socket_client(): SSL: Connection reset by peer 
    Warning: stream_socket_client(): Failed to enable crypto
    


    我希望这可以为某人节省时间!

    【讨论】:

      【解决方案3】:
      function send_iOS_notifications($device_tokens, $notification_content)
          {
              $this->load->library('apn');
              $this->apn->payloadMethod = 'enhance'; // you can turn on this method for debuggin purpose
              $this->apn->connectToPush();
      
                      //$badge_count = $this->set_and_get_ios_active_notifications('',$device_token);
      
              // adding custom variables to the notification
              $this->apn->setData($notification_content);
      
                      $message = $notification_content['message'];
                      if ((strpos($message, 'http://') !== false) || (strpos($message, 'https://') !== false)) {
                          $message = "Image";
                      }
      
              //$send_result = $this->apn->sendMessage($device_token, 'Test notif #1 (TIME:'.date('H:i:s').')', /*badge*/ 2, /*sound*/ 'default'  );
              $send_result = $this->apn->sendMessage($device_tokens, $message, /*badge*/ '', /*sound*/ 'default'  );
      
      
              if($send_result){
                  log_message('debug','Sending successful');
                  $result['status'] = true;
                  $result['message'] = 'Sending successful';
              }
              else{
                  log_message('error',$this->apn->error);
                  $result['status'] = true;
                  $result['message'] = $this->apn->error;
              }
              $this->apn->disconnectPush();
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 2017-04-19
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-18
        • 1970-01-01
        相关资源
        最近更新 更多