Snake
Snake

Reputation: 14678

Google play multi-player, how to receive the invite when the app is closed

The problem I found with google play services (multi-player) is that the invitee has to have the app open already in order to respond to the invite.

Is there no way I can have the invite appear on the invitee phone even if the app is closed (background)?

Upvotes: 2

Views: 2439

Answers (4)

Shakkalakka
Shakkalakka

Reputation: 449

If you send an invite to a player with the game running, the OnInvitationReceivedListener should be triggered from inside the game.

If you send an invite to a player with the game NOT running, Google Play Services on his phone should receive a request. The player can then accept or decline that invitation.

If he accepts the invitation, your game will be launched, and the ConnectionCallbacks.onConnected() will be triggered with a bundle containing the GamesClient.EXTRA_INVITATION. Using this invitation object, you can join the appropriate room.

Please note that I already encountered a bug in Google Play Services SDK ver. 3.2.65 - in which the bundle in the onConnected callback doesn't return the invitation id. This seems to be fixed in 4.0.30.

Upvotes: 3

Akash
Akash

Reputation: 14047

I faced the same problem and I had to address additional conditions. more over, many links in the above answers were broken. So I had to do it on my own and this is how i did it. NB: I was using gamehelper class obtained from sample games. make appropriate changes

public void checkInvites()
{
    if(!mHelper.isSignedIn()) return;
    PendingResult<LoadInvitationsResult> invs = Games.Invitations.loadInvitations(mHelper.getApiClient());
    invs.setResultCallback(new ResultCallback<LoadInvitationsResult>(){
        @Override
        public void onResult(LoadInvitationsResult list) {
            if(list==null)return;
        if(list.getInvitations().getCount()>0)invitationInbox();
        }});
}

This callbacks invitationInbox() method which opens the invitation inbox.

 public void invinbox() 
 {       
         Intent intent = Games.Invitations.getInvitationInboxIntent(mHelper.getApiClient());
            startActivityForResult(intent, RC_INVITATION_INBOX);    }        
    }

The advantage is that you can call checkInvites() from anywhere anytime to check if there are any pending invitations.

Upvotes: 2

Mobile Developer
Mobile Developer

Reputation: 211

I see that onConnected() in my Activity class is not called but it does call the onConnected() method in the GameHelper class.

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 200120

Per the Invitation Acceptance Phase documentation:

The player who initiated the game is automatically joined to the room. Invitees to a multiplayer game will see a notification displayed on mobile devices to prompt them to join the game. If they accept the invitation, your app must issue an API call to join the player to the room corresponding to that invitation.

Play Games services notifies the invitees via the ConnectionCallbacks.onConnected() method. The callback method is passed a connectionHint Bundle. Your app can retrieve an Invitation object from this Bundle by using the GamesClient.EXTRA_INVITATION key.

As explained in the Invite Players option documentation, you can use the selected invitees returned from GamesClient.getSelectPlayersIntent() with RoomConfig.Builder.addPlayersToInvite() to send invitations to the selected players upon room creation which will appear as notifications on other players devices.

Upvotes: 0

Related Questions