I really need some help getting these push notifications to work. I asked about the notifications being sent from the dashboard in a previous post.
I still can’t get these to work, but i am not worried about that at the moment. In the doc for Corona cloud multiplayer, it says calling match:addPlayer( userID, [userAlert]) will push a notification to the other player. I can not seem to get this working.
I know the match is being created from console outputs and the other player is being added, but why no notification.
Here is the test code I have written, maybe someone who has got this working can tell me what I am doing wrong or point me in the right direction.
Thanks, Gooner87
[lua]
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local storyboard = require "storyboard" CC\_Access\_Key = "access\_key" CC\_Secret\_Key = "secret\_key" cloud = require ( "cloud" ) cloud.init( CC\_Access\_Key, CC\_Secret\_Key ) -- gameNetwork = require( "gameNetwork" ) -- local params = { accessKey = CC\_Access\_Key, secretKey = CC\_Secret\_Key, } -- gameNetwork.init( "corona", params ) storyboard.isDebug = true -- Set up some useful utility display variables and functions display.contentBgWidth = 760 display.contentBgHeight = 1140 display.contentBleedWidth = (display.contentBgWidth - display.contentWidth) / 2 display.contentBleedHeight = (display.contentBgHeight - display.contentHeight) / 2 -- function display.center (obj) -- obj.x, obj.y = display.contentCenterX, display.contentCenterY -- end -- function display.zero (obj) -- obj.x, obj.y = 0, 0 -- end --- Login status won't persist after an application exit, to make things simpler for now. local function onSystemEvent (event) if event.type == "applicationSuspend" then collectgarbage("collect") elseif event.type == "applicationExit" then --net.logout() end end Runtime:addEventListener("system", onSystemEvent) --- Reduce the likelyhood of an app load timeout by placing the scene loading/transition on the first frame. local function onEnterFrame (event) Runtime:removeEventListener("enterFrame", onEnterFrame) storyboard.gotoScene("scene2") end Runtime:addEventListener("enterFrame", onEnterFrame) [/lua]
scene2.lua [lua] -- Project: GooglePushNotifications -- -- File name: main.lua -- -- Author: Corona Labs Inc. -- -- This sample app demonstrates how to send and receive push notifications via the -- Google Cloud Messaging service (aka: GCM). -- See the "build.settings" file to see what Android permissions are required. -- See the "config.lua" file on how to register for push notifications with Google. -- See the following website on how to set up your app for Google's push notification system. -- http://developer.android.com/google/gcm/gs.html -- -- Limitations: This sample app only works on an Android device with "Google Play" installed. -- -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2012 Corona Labs Inc. All Rights Reserved. --------------------------------------------------------------------------------------- CC\_Access\_Key = "access\_key" CC\_Secret\_Key = "secret\_key" local cloud = require( "cloud" ) local json = require( "json" ) local GGData = require( "GGData" ) local box = GGData:new( "sample" ) local storyboard = require( "storyboard" ) local toast = require("toast") local scene = storyboard.newScene() local currentAuthToken = "" --variable to store the login 'authToken' cloud.debugEnabled = true local access\_token,fbId,description,editField, editField2, editField3 local token = box.token local id, my\_match\_id, editField, EditField2 print("-----------") print(token) print("-----------") local googleApiKey = "my\_google\_api\_key" -- A Google registration ID is also needed to send push notifications. -- This key will be obtained by the notification listener below after this app has successfully -- registered with Google. See the "config.lua" file on how to register this app with Google. local googleRegistrationId = nil display.setStatusBar(display.DefaultStatusBar) local environment = system.getInfo( "environment" ) if environment == "simulator" then print( "You're in the simulator." ) end local model = system.getInfo( "model" ) print(model) local deviceID = system.getInfo("deviceID") print(deviceID) local btn1,btn2,btn3,btn4,btn5,text,text2,text3,text4,text5,textnew,text6,text7 function scene:createScene( event ) local screenGroup = self.view local authListener = function( event ) -- all events contain event.name, event.type, event.error, event.response. if event.type == "loggedIn" or event.type == "sessionLoggedIn" then print( "User is logged in: ", cloud.isLoggedIn ) -- get the user profile currentAuthToken = cloud.authToken print(currentAuthToken) box:set( "token", currentAuthToken ) box:save() print( box.token ) cloud.registerDevice(system.getInfo("deviceID")) --cloud.unregisterDevice(system.getInfo("deviceID")) print("Printing device ID") print(deviceID) cloud.getProfile() --cloud.getProfile(username) --cloud.updateProfile(points,10) end if event.type == "getProfile" then print( "The user profile: ", event.response ) local response = event.response response = json.decode( event.response ) print(response.username) --print(response.\_id) storyboard.currentUser = response.\_id print(storyboard.currentUser) print(response.points) --print(response.value) textnew = display.newText(response.username, 20, 175, native.systemFont, 24) textnew:setTextColor(255, 0 , 3) screenGroup:insert(textnew) end end --------------------------- cloud.init( CC\_Access\_Key, CC\_Secret\_Key, authListener ) local function onBtnTouch( self, event ) if event.phase == "began" then --local loginParams = {} local loginParams = {} loginParams.type = "user" loginParams.email = editField.text loginParams.password = editField2.text cloud.login( loginParams ) print("touch") return true end end local background = display.newRect(0, 0, display.contentWidth, display.contentHeight) background:setFillColor(128, 0, 0) screenGroup:insert(background) btn1 = display.newRect(20,20, 80, 30) btn1.touch = onBtnTouch btn1:addEventListener("touch",btn1) screenGroup:insert(btn1) text = display.newText("login", 20, 20, native.systemFont, 16) text:setTextColor(0,0,0) screenGroup:insert(text) local multiplayer = cloud.multiplayer local match = cloud.match --'nil' until newMatch() or findMatch() is called -- local function multiplayerListener( event ) -- if ( event.type == "newMatch" ) then -- --handle the new match event -- match:start() -- end -- return true -- end local function multiplayerListener( event ) if ( event.type == "newMatch" ) then --handle the new match event userAlert = "Play me?" match:addPlayer( id , userAlert) match:start() toast.new("Created new game", 3000) elseif ( event.type == "getAllMatches" ) then print("IS it getting here") --process data from the getAllMatches() call local matches = json.decode( event.response ) print(#matches) for i = 1, #matches do local matchID = matches[i].\_id local state = matches[i].state local turn = matches[i].turn\_index local players = matches[i].players[1].state local users = matches[i].players[1].username print( matchID, state, turn ,users, players) --multiplayer.deleteMatch(matches[i]) end cloud.match.data = matches[1] print(matches[1].\_id) elseif ( event.type == "findMatch" ) then --the match table is now available match = cloud.match --'match.data' contains now the match info print( match.data.\_id ) print(match.data.state) end return true end local function onBtnTouch2( self, event ) if event.phase == "began" then print("touch") --multiplayer.getAllMatches() --multiplayer.deleteMatch(matches[i]) storyboard.gotoScene("scene3", "flip", 1000) end return true end print("Made it here") btn2 = display.newRect(20,60, 80, 30) btn2.touch = onBtnTouch2 btn2:addEventListener("touch",btn2) screenGroup:insert(btn2) text1 = display.newText("Matches", 20, 60, native.systemFont, 16) text1:setTextColor(0,0,0) screenGroup:insert(text1) local function onBtnTouch3( self, event ) if event.phase == "began" then print("touch") multiplayer.newMatch() end return true end btn3 = display.newRect(20,100, 80, 30) btn3.touch = onBtnTouch3 btn3:addEventListener("touch",btn3) screenGroup:insert(btn3) text2 = display.newText("Create", 20, 100, native.systemFont, 16) text2:setTextColor(0,0,0) screenGroup:insert(text2) local function onBtnTouch4( self, event ) if event.phase == "began" then print("touch") multiplayer.deleteMatch("51a8ad61c58f8a7cfe00000e") end return true end btn4 = display.newRect(120,20, 80, 30) btn4.touch = onBtnTouch4 btn4:addEventListener("touch",btn4) screenGroup:insert(btn4) text4 = display.newText("Find game", 120, 20, native.systemFont, 16) text4:setTextColor(0,0,0) screenGroup:insert(text4) local function onBtnTouch5( self, event ) if event.phase == "began" then print("touch") userAlert = "Accepted" match:acceptChallenge( userAlert ) end return true end btn5 = display.newRect(120,60, 80, 30) btn5.touch = onBtnTouch5 btn5:addEventListener("touch",btn5) screenGroup:insert(btn5) text5 = display.newText("accept", 120, 60, native.systemFont, 16) text5:setTextColor(0,0,0) screenGroup:insert(text5) multiplayer.setListener( multiplayerListener ) local friends = cloud.friends local searchQuery = "" local function friendsListener( event ) if ( searchQuery == "" ) then return end print(searchQuery) local users = json.decode( event.response ) --print(event.response) --users = json.decode( event.response ) print( #users ) if ( #users == 0 ) then --no users match the query print("No Matches!") text6 = display.newText("No Match Found!", 20, 200, native.systemFont, 20) text6:setTextColor(255,250,205) screenGroup:insert(text6) elseif ( #users == 1 ) then --found a specific match! print("Found match") local userID = users[1].\_id local username = users[1].username local lastName = users[1].last\_name local firstName = users[1].first\_name print( userID, username, lastName, firstName ) id = userID text6 = display.newText("Found match:", 20, 200, native.systemFont, 20) text6:setTextColor(255,250,205) screenGroup:insert(text6) text7 = display.newText(firstName.." "..lastName, 20, 220, native.systemFont, 20) text7:setTextColor(255,250,205) screenGroup:insert(text7) --match:addPlayer( userID ) else --multiple matches found! print(users[1]) text6 = display.newText("Found multiple matches:", 20, 200, native.systemFont, 20) text6:setTextColor(255,250,205) screenGroup:insert(text6) local matchStrings = {} for i = 1, #users do print(searchQuery) if ( users[i].username == searchQuery ) then local userID = users[i].\_id local username = users[i].username local lastName = users[i].last\_name local firstName = users[i].first\_name print( userID, username, lastName, firstName ) local text = string.format(i.." "..firstName.." "..lastName ) matchStrings[i] = display.newText( text, 0, 0, nil, 10 ) matchStrings[i]:setTextColor(255,250,205) --matchStrings[i]:setReferencePoint(display.CenterLeftReferencePoint); matchStrings[i].x = 20 matchStrings[i].y = 220 + i \* 18 screenGroup:insert(matchStrings[i]) break end end end end searchQuery = " " --friends.find( searchQuery ) local function onBtnTouch4( self, event ) if event.phase == "began" then print("touch") searchQuery = editField3.text friends.find( searchQuery ) end return true end btn4 = display.newRect(20,140, 80, 30) btn4.touch = onBtnTouch4 btn4:addEventListener("touch",btn4) screenGroup:insert(btn4) text3 = display.newText("Friends", 20, 140, native.systemFont, 16) text3:setTextColor(0,0,0) screenGroup:insert(text3) friends.setListener( friendsListener ) local function handlerFunction(event) if event.phase == "submitted" then local enteredText = editField.text --enteredText now has the value of the text field. print(enteredText) native.setKeyboardFocus(nil) end end local function handlerFunction2(event) --editField2.isSecure = true if event.phase == "submitted" then local enteredText = editField2.text --enteredText now has the value of the text field. print(enteredText) native.setKeyboardFocus(nil) end end local function handlerFunction2(event) --editField2.isSecure = true if event.phase == "submitted" then local enteredText = editField3.text --enteredText now has the value of the text field. print(enteredText) native.setKeyboardFocus(nil) end end editField = native.newTextField( 10, 350, 200, 36, handlerFunction ) --This sets (prefills) the text field value. --editField.text = "Email" editField2 = native.newTextField( 10, 400, 200, 36,handlerFunction2 ) editField3 = native.newTextField( 10, 440, 200, 36, handlerFunction3 ) --editField2.text = "Password" --editField2.isSecure = true -- Prints all contents of a Lua table to the log. local function printTable(table, stringPrefix) if not stringPrefix then stringPrefix = "### " end if type(table) == "table" then for key, value in pairs(table) do if type(value) == "table" then print(stringPrefix .. tostring(key)) print(stringPrefix .. "{") printTable(value, stringPrefix .. " ") print(stringPrefix .. "}") else print(stringPrefix .. tostring(key) .. ": " .. tostring(value)) end end end end -- Called when a notification event has been received. local function onNotification(event) if event.type == "remoteRegistration" then -- This device has just been registered for Google Cloud Messaging (GCM) push notifications. -- Store the Registration ID that was assigned to this application by Google. googleRegistrationId = event.token -- Display a message indicating that registration was successful. local message = "This app has successfully registered for Google push notifications." native.showAlert("Information", message, { "OK" }) -- Print the registration event to the log. print("### --- Registration Event ---") printTable(event) --cloud.registerDevice(googleRegistrationId) elseif event.type == "remote" then native.showAlert( "message:", event.alert, { "OK" } ) else -- A push notification has just been received. Print it to the log. print("### --- Notification Event ---") printTable(event) end end -- Set up a notification listener. Runtime:addEventListener("notification", onNotification) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) print("Hi") -- net.login() -- facebook.request("me", "GET") --gameNetwork.show( "friends" ) storyboard.printMemUsage() print( "player: enterScene event" ) -- remove previous scene's view -- storyboard.purgeScene( "login" ) -- storyboard.purgeScene( "mid\_scene4" ) end -- Called when scene is about to move offscreen: function scene:exitScene() print( "player: exitScene event" ) --storyboard.purgeScene("player") -- p1Button:removeEventListener("touch", p1Button) -- p2Button:removeEventListener("touch", p2Button) -- shopBtn:removeEventListener("touch", shopBtn) -- infoBtn:removeEventListener("touch", infoBtn) -- p1Button = nil -- p2Button = nil if editField then editField:removeSelf() editField = nil end if editField2 then editField2:removeSelf() editField2 = nil end if editField3 then editField3:removeSelf() editField3 = nil end display.remove(screenGroup) screenGroup = nil end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) print( "((destroying player's view))" ) end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "enterScene" event is dispatched whenever scene transition has finished scene:addEventListener( "enterScene", scene ) -- "exitScene" event is dispatched before next scene's transition begins scene:addEventListener( "exitScene", scene ) -- "destroyScene" event is dispatched before view is unloaded, which can be -- automatically unloaded in low memory situations, or explicitly via a call to -- storyboard.purgeScene() or storyboard.removeScene(). scene:addEventListener( "destroyScene", scene ) --------------------------------------------------------------------------------- return scene [/lua]