Search code examples
javaandroidgoogle-play-servicesgoogle-play-games

Android Google Play notification does not bring user to game screen


Similar questions to this have been trivial at best for answers. I am using a faily up-to-date Google Play Game Services apk 'com.google.android.gms:play-services:7.0.0' and I have onInvitationReceived() and onConnected() implemented.

However, onInvitationReceived() doesn't seem to be called when the invitee accepts the game invite in-game. And while I'm fairly certain onConnected() is called no matter what when the player connects via callback after mGoogleClient.connect() , it seems like the invitation is dropped or something because the player is not redirected to the game screen as I specified if the Bundle contains an invitation (I'm assuming this is called when the app is closed but either way, only a status bar notification is shown for the invite).

Any insight into this would be appreciated. Here are the relevant methods:

onConnected() is from a class, that extends AndroidGame, and AndroidGame extends Activity (I've tried it in both classes, now I just have it overridden in the child class of AndroidGame):

@Override
public void onConnected(Bundle connectionHint) {
    // Connected to Google Play services!
    // The good stuff goes here.
    Games.Invitations.registerInvitationListener(mGoogleClient, Network.getInstance().mListener);

    if (connectionHint != null) {
        Invitation inv =
                connectionHint.getParcelable(Multiplayer.EXTRA_INVITATION);

        if (inv != null) {
            // accept invitation
            RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(Network.getInstance());
            roomConfigBuilder.setMessageReceivedListener(Network.getInstance());
            roomConfigBuilder.setRoomStatusUpdateListener(Network.getInstance());
            roomConfigBuilder.setInvitationIdToAccept(inv.getInvitationId());
            RoomConfig roomConfig = roomConfigBuilder.build();
            Games.RealTimeMultiplayer.join(mGoogleClient, roomConfig);

            // prevent screen from sleeping during handshake
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

            // go to game screen
            setScreen(new CharSelectScreen(this));
        }
    }
}

onInvitationReceived is from my Network class, which is basically a singleton which handles other listeners as well. onInvitationReceived() is both overridden in the Network class and in the anonymous inner class, just to cover my assets:

@Override
    public void onInvitationReceived(Invitation invitation) {
        Intent intent = Games.Invitations.getInvitationInboxIntent(game.getGoogleClient());
        game.startActivityForResult(intent, RC_INVITATION_INBOX);

        mIncomingInvitationId = invitation.getInvitationId();
        AlertDialog.Builder builder = new AlertDialog.Builder(game.getApplicationContext());
        builder.setMessage("Join Game?")
                .setTitle("Bit Game Invitation!");
        builder.setPositiveButton("Join", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                if(!game.getGoogleClient().isConnected()) {
                    game.getGoogleClient().connect();
                }
                // User clicked OK button
                Bundle am = RoomConfig.createAutoMatchCriteria(1, 3, 0);
                RoomConfig.Builder roomConfigBuilder = RoomConfig.builder(Network.getInstance());
                roomConfigBuilder.setMessageReceivedListener(Network.getInstance());
                roomConfigBuilder.setRoomStatusUpdateListener(Network.getInstance());
                roomConfigBuilder.setAutoMatchCriteria(am);
                roomConfigBuilder.setInvitationIdToAccept(mIncomingInvitationId);
                RoomConfig roomConfig = roomConfigBuilder.build();
                Games.RealTimeMultiplayer.join(game.getGoogleClient(), roomConfig);

                // prevent screen from sleeping during handshake
                game.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                dialog.dismiss();

                // go to game screen
                game.setScreen(new CharSelectScreen(game));
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

Solution

  • I solved it, with the short answer being I did not follow the Android Realtime Multiplayer recipe (guidelines) as closely as I should have.

    The guidelines calls for easy access to Google Play sign-in, either automatically when starting the app or I'd imagine preferably, a button to sign in. When I added this, my callback onConnected() code worked, the invitation allowed the user to proceed to the designated game screen with a default waiting room pop-up (implementation shown here). I am still debugging the onInvitationReceived() callback, but I suspect that when the invitation is received when the game is open, the game crash means there is something wrong with my implementation.

    All in all, I think it was a bit of misunderstanding on my end, the documentation is fairly intuitive. Thank you for helping, and I hope this will help future Google Play Game Services developers.