开发者

Android can use like apple push-notification with 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开发者_StackOverflow社区 "sending message :" . $payload . "\n";
    fwrite($fp, $msg);
    fclose($fp);

and andriod have similar way to post with php?

thanks ,all


Take a look at the C2DM service google offers :

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


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜