Android can use as Apple push notification with php?

I use php to publish 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 a similar way of publishing with php?

Thank you all

+3
source share
2 answers

Take a look at the C2DM service offered by google:

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

+1
source

This code is well tested.

Note. You need to remember 3 points to check.

  • Passphrase: Verify with your iOS developer.

  • .PEM: Please make sure your iOS develoepr file is created by .PEM 'for the sandbox or live server.

  • PORT 2195: you need to check if this port is open on the server.

3 , push- .

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

        $ctx = stream_context_create();
        stream_context_set_option($ctx, 'ssl', 'local_cert', '');
        // Put your private key 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

Source: https://habr.com/ru/post/1787886/


All Articles