0
votes

how do I get my app (Android and iOS) receive push notifications, automatically from a control panel, or a webservice ... the only way I found is at the Appcelerator panel, and is not automatic ...

I wish, for example, when there was an update in the database, the push appeared in the user's mobile phone in question ... how can you do this?

Another thing is that how can I do that is generic for android and ios? Is there any module or video lesson to teach to do that?

1

1 Answers

2
votes

For Multi Push Module

This is an part of my php for multi-platform push firing.

    // iOS
    $apnsHost = 'gateway.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'apns-prod.pem';

    //
    $abrirPush = 1;
    $mensagem = array(
        "id_push" => $id_push,
        "id_empresa" => $id_empresa,
        "abrir" => $abrirPush
    );
    $mensagem = json_encode($mensagem);
    $arr['aps'] = array(
        'alert' => $nome_empresa . ' tem uma mensagem para você' , 
        'badge' => 1, 
        'sound' => 'default', 
        'body' => $mensagem
    );
    $payload = json_encode($arr);

    // Abre Conexão iOS
    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

    // Android
    $arrayAndroid = Array();


    // Search for iOS or Android Device
    for ($i = 0; $i < count($montaArray); $i++){

        $pegaToken = $montaArray[$i];

        $contaToken = strlen($pegaToken);


        if ($contaToken < 70){

            $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $montaArray[$i])) . chr(0) . chr(strlen($payload)) . $payload;
            fwrite($apns, $apnsMessage);

        }else{

            /// PUSH ANDROI ////
            array_push( $arrayAndroid, $montaArray[$i]);
        }
    }

    fclose($apns);

    // API access key from Google API's Console ( Server KEY )
    define( 'API_ACCESS_KEY', 'KEY_HERE' );

    // prep the bundle
    $msg = array
    (
        'message'   => 'Tem uma mensagem para você',
        'title'     => $nome_empresa,
        'payload'   => $mensagem,
        'vibrate'   => 1,
        'sound'     => 1,
        'largeIcon' => 'large_icon',
        'smallIcon' => 'small_icon'
    );
    $fields = array
    (
        'registration_ids'  => $arrayAndroid,
        'data'          => $msg
    );

    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
    curl_setopt( $ch,CURLOPT_POST, true );
    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
    $result = curl_exec($ch );
    print $result;
    curl_close( $ch );

note that Apple doesn't alloy an array of tokens like Google does. As Android's token are larger than iOS's Tokens, I count it length to know which token it is. $montaArray comes from my database where all token are stored