I am currently working on a turn based multi player game and using Google Play Game Service for this. All set up has been made. Users can login properly and and select the players to compete with. After the selection when user presses Play button nothing special happens. onActivityResult shows that list of invitees has been prepared and match is created too on the defined criteria. I checked via logs. But on the invitee phone I never got any invitation. I checked on emulator (GenyMotion which serves Google Play Services compared to default emulator of Android SDk) and 2 different Android phones but unable to receive the invitation. I have setup as suggested here
https://developers.google.com/games/services/android/turnbasedMultiplayer
Below is my Code. Kindly anybody look into it and let me know where the problem is.
(P.S. I also came to know Invitations are sent via Google Cloud Messaging and I enabled this service for the game in Developer Console)
MultiPlayerInit.java
import java.util.ArrayList;
import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.google.example.games.basegameutils.BaseGameActivity;
import com.google.android.gms.common.api.*;
import com.google.android.gms.games.*;
import com.google.android.gms.games.Players.LoadPlayersResult;
import com.google.android.gms.games.multiplayer.Invitation;
import com.google.android.gms.games.multiplayer.Invitations;
import com.google.android.gms.games.multiplayer.Multiplayer;
import com.google.android.gms.games.multiplayer.OnInvitationReceivedListener;
import com.google.android.gms.games.multiplayer.Invitations.LoadInvitationsResult;
import com.google.android.gms.games.multiplayer.realtime.RoomConfig;
import com.google.android.gms.games.multiplayer.turnbased.TurnBasedMatchConfig;
import com.google.android.gms.games.multiplayer.turnbased.TurnBasedMultiplayer.InitiateMatchResult;
public class MultiPlayerInit extends BaseGameActivity
implements OnClickListener {
// For our intents
final static int RC_SELECT_PLAYERS = 10000;
final static int RC_LOOK_AT_MATCHES = 10001;
final static int RC_INVITATION_INBOX = 10001;
final static int REQUEST_ACHIEVEMENTS = 10002;
String mIncomingInvitationId;
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.multiplayer_init);
findViewById(R.id.sign_in_button).setOnClickListener(this);
findViewById(R.id.sign_out_button).setOnClickListener(this);
findViewById(R.id.btnInviteFriends).setOnClickListener(this);
findViewById(R.id.btnGetInvitations).setOnClickListener(this);
}
@Override
public void onSignInFailed() {
// Sign in has failed. So show the user the sign-in button.
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
findViewById(R.id.btnInviteFriends).setVisibility(View.GONE);
findViewById(R.id.btnGetInvitations).setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "Sorry! your sign-in attempt failed. Please check your credentials...", Toast.LENGTH_LONG).show();
Log.v("Sign-in Failed","Sorry! your sign-in attempt failed. Please check your credentials...");
}
@Override
public void onSignInSucceeded() {
// show sign-out button, hide the sign-in button
findViewById(R.id.sign_in_button).setVisibility(View.GONE);
findViewById(R.id.sign_out_button).setVisibility(View.VISIBLE);
findViewById(R.id.btnInviteFriends).setVisibility(View.VISIBLE);
findViewById(R.id.btnGetInvitations).setVisibility(View.VISIBLE);
Games.Invitations.registerInvitationListener(getApiClient(), new OnInvitationReceivedListener() {
@Override
public void onInvitationRemoved(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onInvitationReceived(Invitation invitation) {
// TODO Auto-generated method stub
mIncomingInvitationId = invitation.getInvitationId();
Toast.makeText(getApplicationContext(), "You have received an invitation from: "+invitation.getInviter().getDisplayName()+" with ID: " +mIncomingInvitationId, Toast.LENGTH_LONG).show();
}
});
Toast.makeText(getApplicationContext(), "You are now logged in successfully...", Toast.LENGTH_LONG).show();
Log.v("Sign-in Succeeded","You are now logged in successfully...");
// (your code here: update UI, enable functionality that depends on sign in, etc)
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.sign_in_button) {
// start the asynchronous sign in flow
beginUserInitiatedSignIn();
}
else if (view.getId() == R.id.sign_out_button) {
// sign out.
signOut();
// show sign-in button, hide the sign-out button
findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_button).setVisibility(View.GONE);
findViewById(R.id.btnInviteFriends).setVisibility(View.GONE);
findViewById(R.id.btnGetInvitations).setVisibility(View.GONE);
}
else if (view.getId() == R.id.btnInviteFriends) {
Intent intent = Games.TurnBasedMultiplayer.getSelectOpponentsIntent(getApiClient(), 1, 3);
startActivityForResult(intent, RC_SELECT_PLAYERS);
}
else if (view.getId() == R.id.btnGetInvitations) {
Intent intent =
Games.TurnBasedMultiplayer.getInboxIntent(getApiClient());
startActivityForResult(intent, RC_INVITATION_INBOX);
}
}
@Override
protected void onActivityResult(int request, int response, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(request, response, data);
if (request == RC_SELECT_PLAYERS) {
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// get the invitee list
final ArrayList<String> invitees =
data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// get auto-match criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(
Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers
= data.getIntExtra(
Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria
= RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
.addInvitedPlayers(invitees)
.setAutoMatchCriteria(autoMatchCriteria).build();
Games.TurnBasedMultiplayer
.createMatch(getApiClient(), tbmc)
.setResultCallback(new MatchInitiatedCallback());
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xetree.guesspicwithfriends"
android:versionCode="3"
android:versionName="2.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<activity android:name="com.xetree.guesspicwithfriends.HomeActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/AppBaseTheme"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.xetree.guesspicwithfriends.MultiPlayerInit"
android:screenOrientation="portrait" />
</application>
</manifest>
The request does not go out until you call takeTurn()
.