Android Google 播放通知不会将用户带到游戏屏幕

Android Google Play notification does not bring user to game screen

与此类似的问题充其量只是微不足道的答案。我使用的是最新的 Google Play Game Services apk 'com.google.android.gms:play-services:7.0.0',并且我已经实施了 onInvitationReceived()onConnected()

但是,当被邀请者在游戏中接受游戏邀请时,似乎不会调用onInvitationReceived()。虽然我相当确定 onConnected() 被调用,无论玩家在 mGoogleClient.connect() 之后通过回调连接时发生什么,但似乎邀请被丢弃或其他原因,因为玩家没有按照我指定的方式重定向到游戏屏幕如果 Bundle 包含邀请(我假设这是在应用程序关闭时调用的,但无论哪种方式,都只会显示邀请的状态栏通知)。

如果对此有任何见解,我们将不胜感激。以下是相关方法:

onConnected() 来自 class,它扩展了 AndroidGame,而 AndroidGame 扩展了 Activity(我在两个 class 中都尝试过,现在我只是将其覆盖在 AndroidGame 的子 class 中:

@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 来自我的网络 class,它基本上是一个也处理其他侦听器的单例。 onInvitationReceived() 在网络 class 和匿名内部 class 中都被覆盖,只是为了覆盖我的资产:

@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();

我解决了它,简短的回答是我没有像我应该的那样严格遵循 Android Realtime Multiplayer 食谱(指南)。

指南要求轻松访问 Google 播放登录,可以在启动应用程序时自动进行,或者我想最好是一个登录按钮。当我添加这个时,我的回调 onConnected() 代码有效,邀请允许用户使用默认的等候室弹出窗口进入指定的游戏屏幕(显示的实现 here)。我还在调试onInvitationReceived()回调,但是我怀疑是在打开游戏的时候收到邀请,游戏崩溃说明我的实现有问题。

总而言之,我认为这是我的一点误解,文档相当直观。感谢您的帮助,我希望这对未来的 Google Play 游戏服务开发人员有所帮助。