Android可以像苹果一样使用php推送通知吗?

发布于 2024-10-14 21:53:44 字数 1010 浏览 3 评论 0原文

我使用 php 发布到苹果..

$message = $error_msg;

 $deviceToken = $dtoken;
  $徽章= 1;
  $sound = 'received3.caf';
    $body = 数组();
    $body['aps'] = array('alert' => $message);
    如果($徽章)
            $body['aps']['badge'] = $badge;
    如果($声音)
            $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);
    错误报告(E_ALL);
    如果 (!$fp) {
            print "连接 $err $errstr\n 失败";
            返回;
            } 别的 {
           打印“连接正常\n”;
        }
    $payload = json_encode($body);
    $msg = chr(0) 。包(“n”,32)。 pack('H*', str_replace(' ', '', $deviceToken)) 。 pack("n",strlen($payload)) 。 $有效负载;
    打印“发送消息:”。 $有效负载。 “\n”;
    fwrite($fp, $msg);
    fclose($fp);

和andriod有类似的方式用php发布?

谢谢大家

i use php to post to apple ..

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

and andriod have similar way to post with php?

thanks ,all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2024-10-21 21:53:44

看一下 google 提供的 C2DM 服务:

http://code.google。 com/intl/fr-FR/android/c2dm/

Take a look at the C2DM service google offers :

http://code.google.com/intl/fr-FR/android/c2dm/

坚持沉默 2024-10-21 21:53:44

这段代码经过了良好的测试。

注意:您需要记住 3 点进行检查。

  1. 密码:与 IOS 开发人员确认。

  2. .PEM:请验证您的 IOS 开发者创建的“.PEM”文件是否适用于沙箱或实时服务器。

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

如果您已完成这 3 个步骤,现在您可以使用以下代码发送推送通知,只需进行少量配置更改。

 function pushNotification($deviceToken, $msg, $sounds, $type) {

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', '');
        // Put your private key's passphrase here: 
        $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("Failed to connect: $err $errstr" . PHP_EOL);
        $body['aps'] = array(
            'alert'  => $msg,
            'sound'  => $sounds,
            'badge'  => 1,
            'type'   => $type,
        );
        $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));
        // print_r($result);
        fclose($fp);
    }

This code is well tested.

Note : You need to remember 3 points to check.

  1. Passphrase : confirm with IOS developer.

  2. .PEM : Please verify with your IOS develoepr created '.PEM' file is for sandbox or live server.

  3. PORT 2195 : Need to verify whether this port has opened or not on your server.

If you have done these 3 steps, Now you can send push notification with below code with few configuration changes.

 function pushNotification($deviceToken, $msg, $sounds, $type) {

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', '');
        // Put your private key's passphrase here: 
        $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("Failed to connect: $err $errstr" . PHP_EOL);
        $body['aps'] = array(
            'alert'  => $msg,
            'sound'  => $sounds,
            'badge'  => 1,
            'type'   => $type,
        );
        $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));
        // print_r($result);
        fclose($fp);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文