安卓可以像苹果推送通知一样使用PHP吗?

3

我使用PHP向苹果公司发送POST请求。

$message = $error_msg;

  $deviceToken = $dtoken;
  $badge = 1;
  $sound = 'received3.caf';
    $body = array();
    $body['aps'] = array('alert' => $message);
    if ($badge)
            $body['aps']['badge'] = $badge;
    if ($sound)
            $body['aps']['sound'] = $sound;
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', '/home/administrator/applecert/apns-dev.pem');
    $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    error_reporting(E_ALL);
    if (!$fp) {
            print "Failed to connect $err $errstr\n";
            return;
            } else {
           print "Connection OK\n";
        }
    $payload = json_encode($body);
    $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);

Android是否与PHP有类似的发布方式?

谢谢,大家。


这里也遇到了同样的问题,没有任何想法! - Webber Lai
2个回答

1

0

这段代码已经过充分测试。

注意:你需要记住以下三点进行检查。

  1. 口令短语:需要与IOS开发人员确认。

  2. .PEM文件:请确认您的IOS开发人员创建的“.PEM”文件是用于沙盒还是正式服务器。

  3. 端口2195:需要验证该端口在您的服务器上是否已打开。

如果您完成了这3个步骤,现在您可以通过以下代码进行推送通知,并进行少量配置更改。

function pushNotification($deviceToken, $msg, $sounds, $type) {
$ctx = stream_context_create(); stream_context_set_option($ctx, 'ssl', 'local_cert', ''); // 在此处输入您的私钥密码: $passphrase = ; stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); $fp = stream_socket_client( 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx); if (!$fp) exit("连接失败: $err $errstr" . PHP_EOL); $body['aps'] = array( 'alert' => $msg, 'sound' => $sounds, 'badge' => 1, 'type' => $type, ); $payload = json_encode($body); // 构建二进制通知 $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload; // 发送到服务器 $result = fwrite($fp, $msg, strlen($msg)); // print_r($result); fclose($fp); }

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