Notifications plugin custom data

Hello everybody.

Using “plugin.notifications.v2” with FCM on iOS device and the following code to send a notification to specific devices :

sendNotification = function (pToken, pMessage) local jsonMessage if (pToken ~= "unknown") then jsonMessage = [[{ "registration\_ids": ["]] .. tostring (pToken) .. [["], "data": { "alert": "]] .. pMessage .. [[", } }]] -- Send the push notification local url = "https://fcm.googleapis.com/fcm/send" local parameters = { headers = { ["Authorization"] = "key=" .. "MyKeyAuthorization", ["Content-Type"] = "application/json", }, body = jsonMessage, } network.request(url, "POST", onSendNotification, parameters) end end

Now, I want to send custom data. But cannot figure out how to put it in the variable jsonMessage.

Next thing also is how to handle the custom data in the notification listener (event.custom [1] ?)

Any examples will be of great help.

I believe you need a JSON table named “custom” that’s at the same level as your “alert” i.e. inside of “data”. The value of “custom” is whatever key-value pairs you want.

I don’t have code to test this, but I believe that’s how.

Rob

OK Rob,

I tested with the following :

{ "registration\_ids":["xxx"], "data": { "alert":"Alert Message ", "custom": { "messageType":"msgType" } } }

But How to get back the custom data in the notification listener.

I tried the following :

event.custom.messageType

event.custom [“messageType”]

event.custom [1]

All of these return nil

In the GooglePushNotifications sample app:  SampleCode/System/GooglePushNotifications it gives this as the JSON data example:

[[{ "registration\_ids": ["]] .. tostring(googleRegistrationId) .. [["], "data": { "alert": "Hello World!", "sound": "notification.wav", "custom": { "boolean": true, "number": 123.456, "string": "Custom data test.", "array": [true, false, 0, 1, "", "This is a test."], "table": { "x": 1, "y": 2 } } } } ]]

This should populate event.custom according to our documentation.  You might want to table print the entire event table and see what you’re getting. You also have to make sure that FCM isn’t expecting something different, our example was for GCM.

Rob

Hello,

I am back with the results of my testing and wanted to share information :

FCM provide two types of messages (notification messages and data messages)

I have been able to send both types with the following :

-- examples jsonMessageForDataMessage = [[{ "registration\_ids": ["]] .. tostring (pToken) .. [["], "data": { "alert": "Alert message", ["custom"] = { ["score"]= 123 }, } } ]] jsonMessageForNotificationMessage = [[{ "registration\_ids": ["]] .. tostring (pToken) .. [["], "notification": { ["title"] = "Title message", ["body"] = "body message", }, "data": { "alert": "Alert message", } } ]]

In FCM documentation, there is the possibility to send  custom data with a notification message.

It seems that “plugin.notifications.v2” does not handle this.

When I add custom data to notification message, the notification listener gets a table type for event.custom but it is always empty.

Any clues ?

Does the data message work?
 

Yes Data Messages types work.

Notifications Messages work too  (but “plugin.notifications.v2” returns an empty event.custom table in notification listener when custom data sent together with the notification) as below

jsonMessageForNotificationMessage = [[{ "registration\_ids": ["]] .. tostring (pToken) .. [["], "notification": { ["title"] = "Title message", ["body"] = "body message", }, "data": { ["custom"] = { ["score"] = 123 } } } ]]

Can you just use data messages?

@Lourenco

Try using print(json.prettify(event)) at the top of your event listener to see if FCM may be returning the data in some other table key than ‘custom’.

(remember to include local json=require(“json”) at the top of your module)