簡體   English   中英

在iOS中推送通知

[英]push notification in ios

我是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錯誤消息:error:14094410:SSL例程:SSL3_READ_BYTES:sslv3警報握手失敗消息:stream_socket_client():無法啟用加密消息:stream_socket_client():無法連接ssl://gateway.sandbox.push.apple.com:2195(未知錯誤)

創建臨時證書並推送通知以進行開發和分發后。 請遵循生成推送通知的步驟

為了使用生成的證書,您需要創建一個PEM文件 ,該文件同時存儲Apple Push Notification Service SSL證書和私鑰。 您可以從終端創建PEM文件。

導航到包含先前生成的證書和密鑰的目錄,然后執行以下步驟。 此處的文件名反映了作為本課程一部分生成的證書的名稱。 您必須根據授予證書的名稱來更新語法。

首先創建應用程序證書PEM文件。 您可以通過雙擊aps_developer_identity.cer證書文件,然后打開“鑰匙串助手”並將證書導出為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**

這樣就完成了與服務器的通信並驗證了我們的證書是否有效。

在此處輸入圖片說明

我遇到了與您在此處提到的相同的問題。 這花了一些時間和頭來尋找...

我發現糾正了握手錯誤的解決方案是下載委托證書,並使用以下代碼將此證書包括在流上下文中:

$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


我希望這可以為某人節省時間!

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();


    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM