浏览器上的推送通知工作不在命令行上

时间:2018-10-08 11:09:14

标签: php apple-push-notifications apns-php

我有用于运行APN的php文件。当我在浏览器上调用网页时,它确实起作用。但是,在调用

的终端上不起作用
php deneme.php

我确认.pem文件已正确创建。并且还要确定它在php文件中的位置也是正确的(因为在浏览器上可以正常工作)。我用完了选项。我希望你给我一个主意。

deneme.php文件为:

<?php
    $apnsServer = 'ssl://gateway.sandbox.push.apple.com:2195';

    $privateKeyPassword = 'MyPassword123';
    $message = 'Test Push Notifications';

    $deviceToken =
    '2877b691ffd9a3edfa45ee31ff25083f1845e016e7902d130eb09713b1c2ed2f';

    $pushCertAndKeyPemFile =  $_SERVER['DOCUMENT_ROOT'].'/ck.pem';// 'ck.pem';
    $stream = stream_context_create();
    stream_context_set_option($stream,
        'ssl',
        'passphrase',
        $privateKeyPassword);
        stream_context_set_option($stream,
        'ssl',
        'local_cert',
        $pushCertAndKeyPemFile);

    $connectionTimeout = 20;
    $connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
    $connection = stream_socket_client($apnsServer,
    $errorNumber,
    $errorString,
    $connectionTimeout,
    $connectionType,
    $stream);
    if (!$connection){
        echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
        exit;
    } else {
        echo "Successfully connected to the APNS. Processing...</br>";
    }
    $messageBody['aps'] = array('alert' => $message,
        'sound' => 'default',
        'badge' => 2,
        );
    $payload = json_encode($messageBody);
    $notification = chr(0) .
    pack('n', 32) .
    pack('H*', $deviceToken) .
    pack('n', strlen($payload)) .
    $payload;
    $wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
    if (!$wroteSuccessfully){
        echo "Could not send the message<br/>";
    }
    else {
        echo "Successfully sent the message<br/>";
    }
    fclose($connection);
?>

1 个答案:

答案 0 :(得分:1)

从CLI运行时,没有$_SERVER[]超全局变量可用,因为没有服务器在运行。将$_SERVER['DOCUMENT_ROOT'].'/ck.pem';替换为ck.pem文件的绝对路径。

相关问题