简体   繁体   English

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

[英]Android can use like apple push-notification with php?

i use php to post to apple .. 我使用PHP发布到苹果..

$message = $error_msg; $ 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? 和andriod有类似的方式用PHP发布?

thanks ,all 谢谢,所有

Take a look at the C2DM service google offers : 看看谷歌提供的C2DM服务:

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

This code is well tested. 此代码经过了充分测试。

Note : You need to remember 3 points to check. 注意:您需要记住要检查的3个点。

  1. Passphrase : confirm with IOS developer. 密码:与IOS开发人员确认。

  2. .PEM : Please verify with your IOS develoepr created '.PEM' file is for sandbox or live server. .PEM:请与您的IOS开发者验证,创建的“.PEM”文件适用于沙箱或实时服务器。

  3. PORT 2195 : Need to verify whether this port has opened or not on your server. 端口2195:需要验证服务器上是否已打开此端口。

If you have done these 3 steps, Now you can send push notification with below code with few configuration changes. 如果您已完成这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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM