Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Onesignal & Android
Started by agramonte Aug 21 2018 02:57 PM

5 replies to this topic
onesignal android api
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 765 posts
  • Corona SDK

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.

Attached Files


Edited by agramonte, 21 August 2018 - 03:51 PM.


[TOPIC: post.html]
#2

akao

[GLOBAL: userInfoPane.html]
akao
  • Contributor

  • 319 posts
  • Corona SDK

Thx!!



[TOPIC: post.html]
#3

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 765 posts
  • Corona SDK

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.

 

Attached File  Screenshot_20180902-160731-1.png   37.92KB   1 downloads

 

Attached File  Screenshot_20180902-154558.png   184.66KB   1 downloads

 

 

 

 



[TOPIC: post.html]
#4

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 765 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

agramonte

[GLOBAL: userInfoPane.html]
agramonte
  • Contributor

  • 765 posts
  • Corona SDK

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:

 

Attached File  Screen Shot 2018-09-04 at 7.20.29 PM.png   193.76KB   1 downloads

 

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

 

Attached File  oneSignal2.png   30.29KB   1 downloads



[TOPIC: post.html]
#6

akao

[GLOBAL: userInfoPane.html]
akao
  • Contributor

  • 319 posts
  • Corona SDK

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. 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: onesignal, android, api