未收到Google Play游戏服务多人游戏邀请

时间:2014-05-17 08:10:07

标签: android multiplayer invitation gkturnbasedmatch

我目前正致力于基于回合制的多人游戏并使用Google Play游戏服务。所有设置都已完成。用户可以正确登录并选择要与之竞争的玩家。用户按下播放按钮后进行选择后,没有什么特别的事onActivityResult显示已准备好受邀者列表,并且也会根据定义的条件创建匹配。我检查了日志。但是在被邀请者的电话里,我从未收到任何邀请。我检查了模拟器(GenyMotion提供Google Play服务与Android SDk的默认模拟器相比)和2个不同的Android手机但无法接收邀请。我按照这里的建议设置了

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

以下是我的代码。请任何人调查并让我知道问题所在。

(P.S。我也知道通过Google Cloud Messaging发送邀请,我在开发者控制台中为游戏启用了此服务)

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>

1 个答案:

答案 0 :(得分:1)

在你调用takeTurn()之后,请求才会消失。

TurnBasedTurnBasedMultiplayer