Search code examples
androidgoogle-play-games

How to implement custom dialog for accepting turn based multiplayer game invite?


I want to implement a custom dialog box on onInvitationReceived(Invitation invitation) callback. It will have 2 options - 'accept' and 'reject'.

I successfully implemented the 'reject' action. Simplified code below -

@Override
public void onInvitationReceived(Invitation invitation) {
    String invitationId = invitation.getInvitationId();

    if (/*code for selecting 'reject' action*/) {
        Games.TurnBasedMultiplayer.declineInvitation(mGoogleApiClient, invitationId);
    }
}

But how do I implement 'accept' action? Specifically from just Invitation object. I will need TurnBasedMatch object to start the match on invited players end.

The following link from google developers lists only the way to show default view to 'accept' (or 'reject') game.

https://developers.google.com/games/services/android/turnbasedMultiplayer#handling_invitations


Solution

  • I managed to solve this on my own. Below is the simplified code to 'accept' the game invitation and get the match object -

    PendingResult<TurnBasedMultiplayer.InitiateMatchResult> pendingResult = 
        Games.TurnBasedMultiplayer.acceptInvitation(mGoogleApiClient, invitationId);
    
    pendingResult.setResultCallback(new ResultCallback<TurnBasedMultiplayer.InitiateMatchResult>() {
        @Override
        public void onResult(TurnBasedMultiplayer.InitiateMatchResult result) {
            if (result.getStatus().getStatusCode() == GamesStatusCodes.STATUS_OK) {
                TurnBasedMatch match = result.getMatch();
                // do something with match ...
            }
        }
    });