Gaming Services Android SDK

整合 Facebook 登入、Facebook 分享或 Facebook Gaming 後,除非停用自動應用程式事件記錄功能,否則系統會自動記錄並蒐集某些應用程式事件,以供事件管理工具使用。我們建議所有使用 Facebook 登入、Facebook 分享或 Facebook Gaming 的應用程式開發人員瞭解此功能的運作方式。如需深入瞭解系統會蒐集哪些資訊,以及如何停用自動應用程式事件記錄功能,請參閱自動應用程式事件記錄功能

Gaming Services is available as a new component in the official Facebook SDK for Android. Using Facebook official SDKs is the recommended approach and makes it easy to access services enabled by Facebook Login for Gaming including the Player Finder and Sharing (for Gaming) experiences.

Prerequisites

  1. Your application needs to enroll in Gaming Services to access features in this document. Follow the instructions to enroll your application.
  2. Make sure you have the minimal version of Facebook Android SDK or above. Download the latest version.
  3. Ensure your App settings are properly configured for app switch.

Implementation

Facebook Login For Gaming


Facebook Login for Gaming enable a new login method for games. It supports the same implementation and SDK methods as Facebook Login. If this is the first time you're integrating the Facebook SDK into your App, refer to the configuration guide before proceeding.

Instead of receiving public_profile, your application will receive gaming_profile as the default permission. Other requestable permissions include user_friends, email, and gaming_user_picture.


Gaming Graph Domain


After integrating with Facebook Login for Gaming, your application will receive an access token for use on Gaming graph domain (graph.fb.gg), instead of the Facebook graph domain (graph.facebook.com). Read more about gaming graph domain.

You can check if the current User has connected with Facebook Login For Gaming by verifying the Graph Domain to be gaming in the Access Token:

// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
...
import com.facebook.AccessToken;
...
AccessToken currentAccessToken = AccessToken.getCurrentAccessToken();
if (currentAccessToken != null && currentAccessToken.getGraphDomain.equals("gaming")) {
  // current user has been migrated to Facebook Login for Gaming
}

Player Finder

Here's an example of how you would invoke the Player Finder dialog and handle the Dialog Close event using the Android SDK.

// (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
...
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.gamingservices.FriendFinderDialog;
...

public class MainActivity extends Activity {
  ...
  public void openFriendFinderDeepLink(View view) {
    FriendFinderDialog dialog = new FriendFinderDialog(this);
    // if we want to get notified when the dialog is closed
    // we can register a Callback
    dialog.registerCallback(
        this.callbackManager,
        new FacebookCallback<FriendFinderDialog.Result>() {
          @Override
          public void onSuccess(FriendFinderDialog.Result friendFinderResult) {
            Log.e(MainActivity.TAG, "Player Finder Dialog closed");
          }

          @Override
          public void onCancel() {}

          @Override
          public void onError(FacebookException exception) {
            Log.e("GamingServicesFBCallback", exception.toString());
          }
        });
    // open the dialog
    dialog.show();
  }
  ...
}

Sharing for Gaming

This is an example of how you would handle asset uploads (images and videos) and invoke the Share Dialog using the Android SDK.

Depending on how you design the Share experience in your application, it can be a synchronous flow where the user waits for Upload → Share Dialog. Alternatively, you can perform the upload in the background during gameplay asynchronously and allow the user to share at a later stage through their media library on Facebook.

The user will receive a Facebook notification once the asset is uploaded and ready.

  // (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
...
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.gamingservices.GamingImageUploader;
import com.facebook.gamingservices.GamingVideoUploader;
...

public class MainActivity extends Activity {

  private CallbackManager callbackManager;
  private ProgressBar videoUploadProgress;

  ... 

  private void uploadPhoto(Uri imageUri) {
    GamingImageUploader imageUploader = new GamingImageUploader(getApplicationContext());
  	try {
      imageUploader.uploadToMediaLibrary(
          "My Test Image", imageUri, true, new MediaUploadCallback(getApplicationContext(), false));
  	} catch (FileNotFoundException e) {
  		Log.e(MainActivity.TAG, e.toString());
  	}
  }

  private void uploadVideo(Uri videoUri) {
    GamingVideoUploader videoUploader = new GamingVideoUploader(getApplicationContext());
    try {
      videoUploader.uploadToMediaLibrary(
          "My Test Video",
          videoUri,
          new VideoUploadCallback(getApplicationContext(), videoUploadProgress));
    } catch (FileNotFoundException e) {
      Log.e(MainActivity.TAG, e.toString());
    }
  }

}

class MediaUploadCallback implements GraphRequest.Callback {

  private static final String TAG = "MediaUploadCallback";
  private boolean is_video;
  private Context context;


  public MediaUploadCallback(Context context, boolean is_video) {
    this.is_video = is_video;
    this.context = context;
  }

  public void onCompleted(GraphResponse response) {
    if (response.getError() != null) {
      Log.e(MediaUploadCallback.TAG, "Media Upload Failed: " + response.getError().toString());
    } else {
      String mediaID = response.getJSONObject().optString(is_video ? "video_id" : "id");
      CharSequence text = "Upload Complete. Media ID: " + mediaID;
      Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
      toast.show();
    }
  }
}

class VideoUploadCallback extends MediaUploadCallback implements GraphRequest.OnProgressCallback {
  private ProgressBar progressBar;

  public VideoUploadCallback(Context context, ProgressBar progressBar) {
    super(context, true);
    this.progressBar = progressBar;
  }

  // Update an Android ProgressBar with the current upload progress
  public void onProgress(long current, long max) {
    int progress = (int) Math.round(((double) current / (double) max) * 100.0);
    this.progressBar.setProgress(progress);
  }
}