For those that are having problems with the OneSignal API on Android I can confirm this works. I tested it on my PH-1 running 9.0 (Pie).
First the register device function. This can be called every time the user starts the app:
local registerDevice = function() if system.getInfo("environment") == "simulator" then return end local device\_type = 1 if system.getInfo("platform") == "iOS" then device\_type = 0 end local requestBody = {} requestBody.app\_id = "xxxxxxx" -- Your App Id from OneSignal requestBody.device\_type = device\_type requestBody.identifier = notifications.getDeviceToken() requestBody.language = "en" -- In my app I will use the candle light plugin to get language. requestBody.timezone = -18000 -- Hard coded but you should get the time zone of the user. requestBody.game\_version = system.getInfo("appVersionString") requestBody.device\_os = system.getInfo("platformVersion") requestBody.ad\_id = system.getInfo("deviceID") requestBody.device\_model = system.getInfo("architectureInfo") local url = "https://onesignal.com/api/v1/players" local headers = {} headers["Content-Type"] = "application/json" local params = {} params.body = json.encode (requestBody) params.headers = headers network.request( url, "POST", networkListener, params ) -- Listerner end
Called on application start:
local onSystemEvent = function( event ) if ( event.type == "applicationStart" ) then registerDevice() end return true end Runtime:addEventListener( "system", onSystemEvent ) Runtime:addEventListener( "notification", notificationListener )
And finally the listener but it is empty:
local networkListener = function(event) end
Other parts that don’t have to do with OneSignal.
Make sure you require the notification library at the top:
local notifications = require( "plugin.notifications.v2" )
And have the listener for the events coming in (word for word from the documentation):
local notificationListener = function( event ) if ( event.type == "remote" ) then -- Handle the push notification elseif ( event.type == "local" ) then -- Handle the local notification end end
Here is the documentation for the call. There are other values you can send in and some that I have included that you can omit. It does not work if you omit the identifier even though it states it is “recommended” and not t “required”.
And finally, the entire project that I used to test this is attached.
Edit: Corrected a typo that Rob found. Although I didn’t see the error in my testing, it would have been a problem if you tried to test it in iOS.