device-to-device notifications

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?

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)

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)