Android push notifications

John Craig john at splash21.com
Sat Mar 30 17:46:38 CDT 2013


Thanks, Pierre.  The 'title' and 'body' keys in the 'data' payload 
section did the trick.  I'm not getting any badge or sound (yet) - still 
looking into it.
Here's a LiveCode version for anyone interested (and a little json encoder)

on mouseUp
    # devices to send to (up to 1000)
    put "<your device token>" into tTokenA[1]

    # data element of payload
    put "New updates available" into tDataA["title"]
    put "There are 3 new updates..." into tDataA["body"]
    put 3 into tDataA["badge"]
    put "default" into tDataA["sound"]
    put "100" into tDataA["score"]
    put "35" into tDataA["time"]

    # build payload
    put tDataA into tPayloadA["data"]
    put tTokenA into tPayloadA["registration_ids"]
    put "3" into tPayloadA["badge_value"]
    put "SplashCloud" into tPayloadA["collapse_key"]
    jsonEncode tPayloadA, tPayload

    # headers
    put "Content-Type: application/json" & LF into tHeaders
    put "Authorization: key=<your key>" & LF after tHeaders
    set the HTTPHeaders to tHeaders

    # had a couple of errors that were resolved by setting/resetting 
this flag  :/
    #libUrlSetSSLVerification true

    # send the notification
    post tPayload to url "https://android.googleapis.com/gcm/send"
    put it into tResponse
    put the result into tResult
    put the seconds & LF & tResponse & LF & LF & tResult
end mouseUp


# encode array in JSON format
command jsonEncode @pArray, @pJson, pDeep
    if pDeep = empty then put empty into pJson
    put the keys of pArray into tKeys
    repeat for each key tKey in pArray
       if tKey is not an integer then
          put true into tTextKeys
          exit repeat
       end if
    end repeat
    if tTextKeys then
       put "{" into tOpen
       put "}" into tClose
    else
       put "[" into tOpen
       put "]" into tClose
    end if
    put tOpen after pJson
    put 0 into tIndex
    repeat for each line tKey in tKeys
       add 1 to tIndex
       if tIndex > 1 then put comma after pJson
       if tTextKeys then put quote & tKey & quote & colon after pJson
       put pArray[tKey] into tVal
       if tVal is an array then
          jsonEncode tVal, pJson, true
       else if tVal is a number or tVal  is a boolean then
          put tVal after pJson
       else if tVal = empty then
          put "null" after pJson
       else
          replace quote with "\" in tVal
          replace "\" with "\\" in tVal
          replace "/" with "\/" in tVal
          replace numToChar(8) with "\b" in tVal
          replace numToChar(9) with "\t" in tVal
          replace numToChar(10) with "\n" in tVal
          replace numToChar(12) with "\f" in tVal
          replace numToChar(13) with "\r" in tVal
          repeat for each item tCode in 
"0,1,2,3,4,5,6,7,11,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31"
             replace numToChar(tCode) with "\u" & format("%04X", tCode) 
in tVal
          end repeat
          put quote & tVal & quote after pJson
       end if
    end repeat
    put tClose after pJson
end jsonEncode


On 30/03/2013 19:45, Pierre Sahores wrote:
> This script works ok in about displaying what's needed instead of "Notification received" ($abstract) :
>
>> <?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;
>>
>> ?>
> but, on the other side, i can't get any sound to play when the message arrives on the device. Any toughs welcome :-)
>
> Best,
>
> Le 30 mars 2013 à 19:11, John Craig a écrit :
>
>> I've added iOS and Android push notifications to a stack I've been working on.  When sending notifications to my android device, the message displayed when the notification arrives is 'Notification received'.  Does anyone know how to change (if possible) this message to something more meaningful - I've not found this so far in google's docs.
>>
>> TIA
>>
>> John.
>>
>> _______________________________________________
>> 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
>
>
> _______________________________________________
> 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
>




More information about the use-livecode mailing list