mobile push notifications how-to

Pierre Sahores sc at sahores-conseil.com
Mon Feb 4 16:03:19 EST 2013


In response to your previous question :

1.- The client side HTTP REST POST LC-client-side app button :

> global DevicesList,MIAD
> 
> on mouseUp
>    beep
>    if pm_length() <= 138 then 
>        put "Envoi du message Push à tous les prospects..." into fld "prompt"
>       set httpHeaders to "Content-type: application/x-www-form-urlencoded" & return
>       get url MIAD
>       post URLEncode("authentpostkey=" & hexDigest("authentpostkeyvalue") & "&message=push&devices=" & DevicesList & "&badge=" & trim(fld "message_badge") & \
>             "&title=" & trim(utf8encode(bslasher(fld "message_title"))) & "&abstract=" & trim(utf8encode(bslasher(fld "message_abstract")))) & \
>             "&payload=" & trim(utf8encode(bslasher(fld "message_payload"))) to URL MIAD
>       put "Aucun traitement en cours..." into fld "prompt"
>    else answer "Erreur : votre message comporte" && pm_length() && "pour une limite APNS (Apple Push Notifications Server) supportée de 138 caractères." && \
>          "Merci de corriger votre message pour éviter qu'il ne soit automatiquement rejeté par le serveur APNS de distribution de votre message vers les devices iOS"
> end mouseUp

where DevicesList contains the iOS and Android IDs of the devices you want to push the message too (out of a server-side SQL table you need to setup and interact with in HTTP REST POST for best results), MIAD targets the http path of the mobile push.lc script (see below) and function pm_length() verifies that the push message length don't exceeds the APNS practical allowed limit.

2.- mobile push.lc : the main server-side lc script to install in the cgi-bin directory (PostIn contains the client-side HTTP REST POST DATA) :

>             split PostIn by "&" and "="
>             
>             ### APNS maxi : 100 000 par envoi ### --> Apple Push Notification Server
>             put url ("file:" & local path to ipush.php") into tpushvar        
>             put tpushvar into tpushtmp
>             put "0" into tcpteur
>             repeat for each line l in PostIn["devices"]
>                if length(l) is "64" then
>                   put "$devices[" & tcpteur & "] = '" & l & "';" & return after lesdevices
>                   add 1 to tcpteur
>                end if
>             end repeat
>             replace "###devices###" with "$devices = Array();" & return & trim(lesdevices) in tpushvar
>             replace "###title###" with "$title = '" & PostIn["title"] & "';" in tpushvar
>             replace "###abstract###" with "$abstract = '" & PostIn["abstract"] & "';" in tpushvar
>             replace "###payload###" with "$payload = '" & PostIn["payload"] & "';" in tpushvar
>             replace "###badge###" with "$badge =" && PostIn["badge"] & ";" in tpushvar
>             replace "###authent###" with "$ck = 'path to your APNS private key';" in tpushvar
>             replace "###passwd###" with "$passphrase = 'your APNS passphrase';" in tpushvar
>             put tpushvar into url ("file:" & local path to ipush.php")
>             get url ("http path to ipush.php")
>             put tpushtmp into url ("file:" & local path to ipush.php")
>             put it into apnsreply
>             
>             ### GPNS maxi : 1000 par tranche envoyée ### --> Google Push Notification Server
>             put "" into lesdevices
>             put url ("file:" & local path to apush.php") into tpushvar        
>             put tpushvar into tpushtmp
>             repeat for each line l in PostIn["devices"]
>                if length(l) is not "64"
>                then put setquote(l) & "," after lesdevices
>             end repeat
>             put "1" into tranchenumber
>             put "1" into tranchestart
>             put "" into devicestranche[1]
>             if the num of items in lesdevices > 1000 then
>                repeat with c = 1 to 1+(the num of items in lesdevices div 1000)
>                   put item tranchestart to 999+tranchestart of lesdevices into devicestranche[tranchenumber]
>                   add 1 to tranchenumber
>                   add 1000 to tranchestart
>                end repeat
>             end if
>             if devicestranche[1] is "" then
>                replace "###devices###" with char 1 to -2 of lesdevices in tpushvar
>                replace "###title###" with PostIn["title"] in tpushvar
>                replace "###abstract###" with PostIn["abstract"] in tpushvar
>                replace "###payload###" with PostIn["payload"] in tpushvar
>                replace "###badge###" with PostIn["badge"] in tpushvar
>                replace "###authent###" with "yourGPNSKey" in tpushvar
>                put tpushvar into url ("file:" & local path to apush.php")
>                get url ("http path to apush.php")
>                put tpushtmp into url ("file:" & local path to apush.php")
>                return it & return & apnsreply
>             else
>                repeat with c = 1 to -1+tranchenumber
>                   replace "###devices###" with devicestranche[c] in tpushvar
>                   replace "###title###" with PostIn["title"] in tpushvar
>                   replace "###abstract###" with PostIn["abstract"] in tpushvar
>                   replace "###payload###" with PostIn["payload"] in tpushvar
>                   replace "###badge###" with PostIn["badge"] in tpushvar
>                   replace "###authent###" with "yourGPNSKey" in tpushvar
>                   put tpushvar into url ("file:" & local path to apush.php")
>                   get url ("http path to apush.php")
>                   put tpushtmp into tpushvar
>                end repeat
>                put tpushtmp into url ("file:" & local path to apush.php")
>                return it & return & apnsreply
>             end if


3.- Both those files need to be installed on your server :

ipush.php :

> <?php
> 
> // gateway.sandbox.push.apple.com:2195
> // gateway.push.apple.com:2195
> 
> ###devices###
> ###title###
> ###abstract###
> ###payload###
> ###badge###
> ###authent###
> ###passwd###
> 
> $ctx = stream_context_create();
> stream_context_set_option($ctx, 'ssl', 'local_cert', $ck);
> 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' => $abstract,'badge' => intval($badge),'sound' => 'default');
> $body['title'] 		= $title;
> $body['body'] 		= $abstract;
> $body['payload'] 	= $payload;
> 
> $fields = json_encode($body);
> 
> $i=0;
> while($devices[$i] != '') {
> 	$msg = chr(0) . pack('n', 32) . pack('H*', $devices[$i]) . pack('n', strlen($fields)) . $fields;
> 	
> 	$result = fwrite($fp, $msg, strlen($msg));
> 	
> 	if (!$result) echo '0' . PHP_EOL;
> 	else echo 1+$i . PHP_EOL;
> 
>   	$i++;
> }
>   
> fclose($fp);
> 
> ?>



apush.php :

> <?php
> 
> $registrationIDs 	= Array(###devices###);
> $title 				= "###title###";
> $abstract 			= "###abstract###";
> $payload 			= "###payload###";
> $badge 				= "###badge###";
> $apiKey			 	= "###authent###";
> $url 				= "https://android.googleapis.com/gcm/send";
> 
> $fields = array(
> 	'registration_ids' => $registrationIDs,
> 	'collapse_key' => 'abcdef',
> 	'data' => array('title' 	=> $title,
> 					'alert' 	=> $abstract,
> 					'body' 		=> $abstract,
> 					'payload' 	=> $payload,
> 					'badge' 	=> intval($badge),
> 					'sound' 	=> 'default'
> 					)
> );
> 
> $headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
> 
> $ch = curl_init();
> 
> curl_setopt($ch, CURLOPT_URL, $url);
> curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
> curl_setopt($ch, CURLOPT_POST, true);
> curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
> 
> $result = curl_exec($ch);
> 
> curl_close($ch);
> 
> echo $result;
> 
> ?>

As you can see, the Google push way to go is lots more advanced than the Apple's one, thousand times more advanced to be precise...

HTH,

Le 1 févr. 2013 à 19:59, Chris Sheffield a écrit :

> Does anybody have a how-to guide for using  push notifications? I've read through the dictionary and the iOS release notes about how to handle received notifications, but how does one actually send out a message? The docs mention a "Push Notification Server" and registering for the service, but I don't understand how you do that. Is it done through iTunes Connect somewhere?
> 
> I'm probably just a little thick today, but I can't seem to figure this out.
> 
> Thanks,
> Chris
> 
> 
> 
> --
> Chris Sheffield
> Read Naturally, Inc.
> www.readnaturally.com
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

--
Pierre Sahores
mobile : 06 03 95 77 70
www.sahores-conseil.com





More information about the use-livecode mailing list