Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

device-to-device notifications
Started by Lourenco May 01 2014 03:58 AM

1 reply to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Lourenco

[GLOBAL: userInfoPane.html]
Lourenco
  • Contributor

  • 177 posts
  • Corona SDK

I am trying to use the api as defined here http://docs.gamethrive.apiary.io

 

Should I first create a player with the token returned by APNS

 

https://gamethrive.com/api/v1/players?app_id=xxx&device_type=0&identifier=applepushtoken

 

Is this the correct way to access the api?

 

 



[TOPIC: post.html]
#2

george18

[GLOBAL: userInfoPane.html]
george18
  • Contributor

  • 233 posts
  • Corona SDK

Hi Lourenco,

 

Not quite. That URL is for registering players, but in this case you'll want to use the one for sending a notification to someone.

 

First, you'll need to store the recipient's GameThrive Player ID somewhere. You can get the current player's gamethrive id by calling:

 

function IdsAvailable(playerID, pushToken)
  print("PLAYER_ID:" .. playerID)
end

GameThrive.IdsAvailableCallback(IdsAvailable)

 

Put this somewhere in your main.lua file after where you require the GameThrive plugin. IdsAvailable will be called just after a player starts your app.

 

Next, you'll need to send that playerId to your server somewhere (You can use a service like Parse or your own hosting).

 

Finally, when a player needs to send a notification to another player, the sender's device will need to ask your server to send them the target's playerId.

 

Once the sender has the target's playerId, the sender device can call a method on our server to send a notification:

 

HTTP METHOD: POST

 

URL: https://gamethrive.com/api/v1/notifications

 

REQUEST HEADER:

Content-Type: application/json

 

REQUEST BODY:

{"app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c",
"contents": {"en": "George challenged you to beat his score!"},
"isAndroid": true,
"isIos": true,
"include_player_ids": ["(RECIPIENT_PLAYER_ID_HERE)"]}

 

 

Here's how you would do it in LUA (This shows you how to send a notification through to our server, not the code you would need to store/fetch the playerId of the person you want to send the notification to)

 


function SendJson(url, action, in_json, callback)
  local json = require ( "json" )
  local headers = {}
  headers["Content-Type"] = "application/json"
  headers["Accept-Language"] = "en-US"

  local params = {}
  params.headers = headers
  params.body = json.encode( in_json )

  network.request ( url, action, callback, params )
end

local function networkListener( event )
  if ( event.isError ) then
    print( "Network error!")
  else
    print ( "RESPONSE: " .. event.response )
  end
end
local jsonToSend = {["app_id"] = "5eb5a37e-b458-11e3-ac11-000c2940e62c",
                    ["contents"] = {["en"] = "George challenged you to beat his score!"},
                    ["isAndroid"] = true,
                    ["isIos"] = true,
                    ["include_player_ids"] = ["(RECIPIENT_PLAYER_ID_HERE)"]}


SendJson("https://gamethrive.com/api/v1/notifications", "POST", jsonToSend, networkListener)



[topic_controls]
[/topic_controls]