PHP中的推送通知

9

在尝试执行我的PHP脚本发送推送通知到我的iPhone后,我遇到了这个错误。

我已经尝试了所有方法,但是仍然无法解决。我认为这意味着我的ck.pem是错误的,但我不确定是key.pem还是cert.pem出了问题。

请帮忙解决。

脚本

    // This this a fake device id:
$deviceToken = '9870h8v088bj29u080af894jj67klfgcv9mmm79k8e4l23456h908743n093e359';

// fake password:
$passphrase = '123456';

// Put your alert message here:
$message = 'New Message';
   ////////////////////////////////////////////////////////////////////////////////

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $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);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
                     'alert' => $message,
                     'sound' => 'default',
                     'badge' => '1'
                     );

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

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

// Close the connection to the server
fclose($fp);

?>

错误

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown in     /Users/daveking/Desktop/App Certificates/simplepush.php on line 21

Warning: stream_socket_client(): Failed to enable crypto in /Users/daveking/Desktop/App Certificates/simplepush.php on line 21

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/daveking/Desktop/App Certificates/simplepush.php on line 21
Failed to connect: 0 

可能是重复问题:https://dev59.com/1Ww15IYBdhLWcg3w0fKh - Tim Withers
1
@TimWithers 这并没有帮助到我。虽然它们都涉及推送通知,但并不完全相同。 - BigT
也许你的有效载荷大小超过了苹果允许的256字节,因此可能会导致fwrite或此错误 - 我遇到了这个问题。 - Aditya P Bhatt
4个回答

6

这些是我在创建ck.pem时所遵循的教程。我已经使用了上面的simplepush.php,但像我说的那样,我仍然遇到了那些错误。 - BigT
刚刚搞定了。我的 cer 文件不知怎么就损坏了,所以我重装了它并重新学习了那些教程。谢谢! - BigT

4
可以在以下链接中找到描述步骤的好文章:http://blog.boxedice.com/2009/07/10/how-to-build-an-apple-push-notification-provider-server-tutorial/ 要了解证书问题,可以查看Urban Airship网站:http://urbanairship.com/docs/keys.html 关于审核方面,需要知道:
=>app signed with a dev cert = sandbox url & dev apns cert, app signed with

=>appstore/adhoc cert = prod url & prod apns cert

在之前使用过开发应用程序的设备上使用临时/应用商店应用程序也会导致springboard崩溃。(因此基本上需要两台设备)(待确认。)

重要提示:必须保持与沙盒的连接,即不能连接、发送推送、断开连接。如果这样做,苹果可能会将您视为可能的DDoS攻击。

从服务器触发推送通知的PHP示例脚本可能如下所示:

<?php

// from http://www.macoscoders.com/2009/05/17/iphone-apple-push-notification-service-apns/
// call: /apns/apns.php?message=Hello%20from%20macoscoders&badge=2&sound=received5.caf

$deviceToken = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';                        

// Passphrase for the private key (ck.pem file)
// $pass = ;
// Get the parameters from http get or from command line

$message = $_GET['message'] or $message = $argv[1] or $message = 'Message sent ' . @date("H:i:s d/M/Y", mktime());
$badge = (int)$_GET['badge'] or $badge = (int)$argv[2] or $badge = 111;
$sound = $_GET['sound'] or $sound = $argv[3] or $sound = 'chime';

// Construct the notification payload
$body = array();
$body['aps'] = array('alert' => $message);
if ($badge)
    $body['aps']['badge'] = $badge;
if ($sound)
    $body['aps']['sound'] = $sound;
/* End of Configurable Items */

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'push/apns-dev.pem');

// assume the private key passphase was removed.
// stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);

if (!$fp) {
    print "Failed to connect $err $errstr\n";
    return;
} else {
    print "Connection OK

";
}

$payload = json_encode($body);

// request one 
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', , $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "sending message :" . $payload . "\n";

fwrite($fp, $msg);

fclose($fp);

?>

3

这里是可用的代码:

<?php

$deviceToken = '8845ba7c41e95e12caea6381ea6f01b5cd7b59a52feb9005e0727a65a4105dc2a0';

$passphrase = '';

$message = 'Your message';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $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);

echo 'Connected to APNS' . PHP_EOL;


$body['aps'] = array(
    'alert' => array(
        'body' => $message,
        'action-loc-key' => 'Bango App',
    ),
    'badge' => 2,
    'sound' => 'oven.caf',
    );

$payload = json_encode($body);

// Build the binary notification
$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);

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接