pushNotificationReceived

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
JereMiami
Posts: 120
Joined: Mon Nov 03, 2014 12:17 am

pushNotificationReceived

Post by JereMiami » Sat Oct 29, 2022 1:33 am

How do you handle pushNotificationReceived for android?

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
   
4. And I do indeed receive a push notification message back from the server on the device with pushNotificationReceived like so:

Code: Select all

on pushNotificationReceived tMessage
   answer "Push Notification:" && quote & tMessage & quote with "Okay"
end pushNotificationReceived
But the answer is Push Notification: "". Any thoughts on why tMessage is empty? What is tMessage supposed to be? Do we need to decode tMessage?

JereMiami
Posts: 120
Joined: Mon Nov 03, 2014 12:17 am

Re: pushNotificationReceived

Post by JereMiami » Sat Oct 29, 2022 11:44 am

So this is what I learned. Firebase handles push notifications differently depending on if the app is open in the foreground, open in the background, or closed completely. Thus, the pushNotificationReceived handler gives the notification only when the app is closed or open in the background- but not open in the foreground. For me, the tMessage parameter is not an array, json, or anything else but empty if the app is open in the foreground.

simon.schvartzman
Posts: 641
Joined: Tue Jul 29, 2014 12:52 am
Location: Brazil

Re: pushNotificationReceived

Post by simon.schvartzman » Sat Oct 29, 2022 10:44 pm

Hi JereMiami, please see if the following remarks helps

Code: Select all

 -- next line update from "payload" to "livecode.payload" with LC 9.6.5
   put pPayload into tDataA["livecode.payload"]
   -- next lines introduced with LC 9.6.5
   local tNotifA 
   put pTitle into tNotifA["title"] 
   put pBody into tNotifA["body"] 
   put tNotifA into tNotificationA["notification"]
Also due to the fact that, as you have already found out, the handling is different if the notification arrives when the APP is open than when it is close, and moreover iOS than Android handles it slightly different, in my case, I used the following "trick" in order to make everything uniform:

To send a notification:
- upload the "payload" to a very simply mySQL DB where I have basically 3 fields:
-- Token ID of the device to receive the notification
-- payload
-- status (which could be sent or read)
- send the notification where the payload is a doesn't matter because is not going to be use

When a notification is received the device:
- access the mySQL DB and retrieves the last record matching its Token ID
- updates the status field to "read"
- shows the payload to the user

Hope it helps, regards
Simon
________________________________________
To ";" or not to ";" that is the question

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”