This is what I have got so far:
1. Setup Firebase and send a push notification from the cloud. See here: https://lessons.livecode.com/m/4069/l/1 ... code-9-6-5. Very easy.
2. Now I want to send a push notification from Livecode to a device. See here: https://lessons.livecode.com/m/4069/l/5 ... th-android and here: (viewtopic.php?f=53&t=32860&p=181449&hil ... ed#p181449. A little harder.
3. I receive a successful result when sending the push notification from Livecode to the device which look something like this: {"multicast_id":674397827141276168,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1614424620452672%f32a374f90939706"}]}. So the message is definitely sent. (Note: The code to send a push notification from livecode to the server is from the earlier lesson cited above).
Code: Select all
put "XXXXX" into tAPIKey
put "This is the title." into tTitle
put "This is the body." into tBody
put 1 into tBadge
put "true" into tSound
put "This is the payload." into tPayload
pushNotificationAndroid tAPIKey, gDevices, tTitle, tBody, tBadge, tSound, tPayload
command pushNotificationAndroid pAPIKey, pDevices, pTitle, pBody, pBadge, pSound, pPayload
local tDataA, tNotificationA, tNotification, tHeaders
# build data element of notification
put pTitle into tDataA["title"]
put pBody into tDataA["body"]
put pBadge into tDataA["badge_value"]
put pSound = true into tDataA["play_sound"]
put pPayload into tDataA["payload"]
# build notification
put tDataA into tNotificationA["data"]
split pDevices with LF
put pDevices into tNotificationA["registration_ids"]
put "Splash21" into tNotificationA["collapse_key"]
# create a JSON encoded message
jsonEncode tNotificationA, tNotification
# headers
put "Content-Type: application/json" & LF into tHeaders
put "Authorization: key=" & pAPIKey & LF after tHeaders
set the HTTPHeaders to tHeaders
# had a couple of errors that were resolved by setting/resetting this flag :/
libUrlSetSSLVerification false
# send the notification
# URL updated on 28 Feb 2021
post tNotification to url "https://fcm.googleapis.com/fcm/send"
return it
end pushNotificationAndroid
# encode array in JSON format
command jsonEncode @pArray, @pJson, pDeep
local tKeys, tTextKeys, tOpen, tClose, tIndex, tVal
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
Code: Select all
on pushNotificationReceived tMessage
answer "Push Notification:" && quote & tMessage & quote with "Okay"
end pushNotificationReceived