Need Help with push notifications

Is it possible to send Push Notification using DF Server Side scripting.
Connecting APNS and GCM for Android from DSP would be needed.

I want it to be called on Insert and Delete for a certain Remote SQL Table.

Any help in this regard, or any sample code somewhere would be very helpful.

The DF roadmap says they should have notifications this year. I’m guessing it will be in version 2.0

What is the best way to do it now using DSP?
I mean before that version comes.

I’d set up a couple of PHP scripts on your server and call them using a server side scripts on the insert and delete operations. Assuming you have the device key and know the platform you could perform some simple logic to call the relevant push messaging handler.

Sample for PHP for GCM:

<?php

// Message to send
$message      = "the test message";
$tickerText   = "Hello there sailor";
$contentTitle = "Test Push";
$contentText  = "Test Content";
 
$registrationId = 'device_id';
$apiKey = "yourkey";
 
$response = sendNotification(
               $apiKey,
               array($registrationId),
               array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );
 
echo $response;


/**
 * The following function will send a GCM notification using curl.
 *
 * @param $apiKey        [string]                   The Browser API key string for your GCM account
 * @param $registrationIdsArray [array]          An array of registration ids to send this notification to
 * @param $messageData [array] A named array of data to send as the notification payload
 */
 
function sendNotification( $apiKey, $registrationIdsArray, $messageData )

{ 
   $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
   $data = array(
       'data' => $messageData,
       'registration_ids' => $registrationIdsArray
   );
 
   $ch = curl_init();
 
   curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
   curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
   curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
   curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
   curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
 
   $response = curl_exec($ch);
   curl_close($ch);
 
   return $response;
   
}

?>

Sample for APNS:

<?php

// Message to send

$message = 'the test message';


// Put your device token here (without spaces):

$deviceToken = 'device_id';



//

//        Certificate Key Details

$certfile = 'your_cert';

// Put your private key's passphrase here:

$passphrase = 'key_password';


////////////////////////////////////////////////////////////////////////////////


$ctx = stream_context_create();

stream_context_set_option($ctx, 'ssl', 'local_cert', $certfile);

stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);


// Open a connection to the APNS server

$fp = stream_socket_client(

        'ssl://gateway.push.apple.com:2195', $err,

        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);


if (!$fp)

        exit("Failed to connect: $err $errstr" . PHP_EOL);


echo 'Connected to APNS' . PHP_EOL;


// Create the payload body

$body['aps'] = array(

        'alert' => $message,

        'badge' => 0,

        'sound' => 'default'

        );


// Encode the payload as JSON

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


if (!$result)

        echo 'Message not delivered' . PHP_EOL;

else

        echo 'Message successfully delivered' . PHP_EOL;


// Close the connection to the server

fclose($fp);

?>
1 Like

Great… so will these php scripts also be hosted in DSP?
Or there needs to be another server which needs to host these scripts which call the APNs/GCM?

They don’t have to be. However as the server hosting the DSP will be running PHP you might as well just create a directory where they can be executed from, it’d probably be quicker and more secure too as they can then just be run as localhost.

Thanks Antony!
I will try to work on this. In case of any further help will let you know.