Onesignal & Android

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.

Thx!!

I created a small file that encapsulates the call to OneSignal and I created a project where you can run it. You can find both the file and instructions here:

https://github.com/agramonte/Corona—OneSignal-Example/blob/master/README.md

It only works on Android, but I am testing on iOS tomorrow and hopefully will have it running also. Surprisingly the notification icons also works. I didn’t expect it since some have mentioned that it doesn’t work with Firebase.

It is now working for iOS also. I have updated gitHub.

This will be my last update on this for a while, I have to move on to other parts of my game. I made some small changes for error checking and fixed typos in the documentation. 

Finally here is a picture of it running on my iOS device and my game:

And here are my two test devices in the OneSignal contral panel:

Nice! Thanks for doing this. If oneSignal doesn’t get their act together in the next week or 2, we are going to give this approach a try. 

Thx!!

I created a small file that encapsulates the call to OneSignal and I created a project where you can run it. You can find both the file and instructions here:

https://github.com/agramonte/Corona—OneSignal-Example/blob/master/README.md

It only works on Android, but I am testing on iOS tomorrow and hopefully will have it running also. Surprisingly the notification icons also works. I didn’t expect it since some have mentioned that it doesn’t work with Firebase.

It is now working for iOS also. I have updated gitHub.

This will be my last update on this for a while, I have to move on to other parts of my game. I made some small changes for error checking and fixed typos in the documentation. 

Finally here is a picture of it running on my iOS device and my game:

And here are my two test devices in the OneSignal contral panel:

Nice! Thanks for doing this. If oneSignal doesn’t get their act together in the next week or 2, we are going to give this approach a try. 

Thanks man, your help is much appreciated.

1 Like