Login with Facebook🔗

Login with Facebook - allows people to quickly create an account and enter your application from various platforms. This feature is available on iOS, Android, web apps, PC apps, and devices like Smart TV and IoT. Logging in with Facebook allows you to solve two problems: perform authentication and request permissions to access people's data. Logging in through Facebook can be used simply for authorization, and for authorization and requesting permissions. Using Facebook Login, people can quickly and easily create an account in your application without setting a password. This simple and convenient method allows you to increase conversion. Having created an account on one platform, a person can then log in to your application on any other platforms - just one touch or click. A verified email address will allow you to reach that person and engage them again. Developers who integrated Facebook login into their applications were able to significantly increase the number of login operations and increase the level of user engagement - especially since people themselves more often use Facebook login.
Important
This module works without depending on Facebook SDK, but at the same time is compatible with it when updating. If Facebook SDK is currently being used, after switching to MRGS, the current sessions will be automatically found in cache, even if Facebook SDK was removed from the project.
Android/Unity
Don't use Android gradle plugin classpath 'com.android.tools.build:gradle:x.x.x' lower3.4.3 version as it contains bugs in Jetifier and doesn't allow **CustomTabs* * due to compilation errors. Learn more.
Preset🔗
Obtaining keys and necessary data🔗
Before begin, set up a developer account on Facebook, add a new application. After registering the application, you will receive a unique identifier for your application (Facebook AppID). Also, if you want to use some additional user permissions, such as access to your friends list, getting a custom birthday, gender, city and location, then you need to configure these permissions in the application settings, and then go through review from the facebook (we will request all additional information automatically, and if there are permissions, all data will automatically appear in the MRGSAuthenticationUser object). Without a review, only Email can be requested. Instructions for registering the project and obtaining the necessary keys can be found on the Facebook site - https://developers.facebook.com/apps/.
Important
At this step, you only need to get Facebook App ID and configure the application on the Facebook site. How to use it in the project is described below.
Android/Unity
Don't use Android gradle plugin classpath 'com.android.tools.build:gradle:x.x.x' lower than3.4.3 version because it contains errors in Jetifier and does not allow the use of CustomTabs due to compilation errors. Learn more.
Project setup🔗
In order for authorization to work correctly, you need to add some changes to the project:
For Unity
For iOS, you need to add the following data to the Info.plist application:
- Add new schemes to open other applications from your application
- A new URL scheme for opening an application from outside of the form
fb <FACEBOOK_APP_ID>
You need to add a new application opening scheme so that the browser can return the user back to your application. The URL scheme should be a string of the form fb<FACEBOOK_APP_ID>, where FACEBOOK_APP_ID is your Facebook application identifier obtained in the previous step (for example, if FACEBOOK_APP_ID is 1595275917403961, then the added scheme should look like: fb1595275917403961 ) Details on adding URL schemes in the generated project are described in this article, but Unity has a simpler method for adding data to the application plist described below.
Schemes for opening other applications are needed to check the availability of the official facebook application. The following schemes must be added - fbapi,fbauth, fbauth2 in LSApplicationQueriesSchemes. Details on adding URL schemes in the generated project are described in this article, but Unity has a simpler method for adding data to the application plist, described below.
To do this, add the code to the Unity post build process:
[PostProcessBuild]
public static void PostProcessBuild(BuildTarget target, string path)
{
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
PlistElementArray schemesToOpen = rootDict.CreateArray("LSApplicationQueriesSchemes");
schemesToOpen.AddString("fbapi");
schemesToOpen.AddString("fbauth");
schemesToOpen.AddString("fbauth2");
// Addibg URL-scheme
PlistElementArray appSchemes = rootDict.CreateArray("CFBundleURLTypes");
PlistElementDict appSchemesDict = appSchemes.AddDict();
appSchemesDict.SetString("CFBundleURLName", "");
PlistElementArray appSchemesArray = appSchemesDict.CreateArray("CFBundleURLSchemes");
appSchemesArray.AddString("fb<FACEBOOK_APP_ID>"); // Where FACEBOOK_APP_ID is your Facebook App ID obtained in the previous step
File.WriteAllText(plistPath, plist.WriteToString());
}
Or modify the existing property adding code in the application's Info.plist, if you have one.
For iOS
You need to add the following data to the Info.plist application:
- Add new schemes to open other applications from your application
- A new URL scheme for opening an application from outside of the form
fb <FACEBOOK_APP_ID>
You need to add a new application opening scheme so that the browser can return the user back to your application. The URL scheme should be a string of the form fb<FACEBOOK_APP_ID>, where FACEBOOK_APP_ID is your Facebook application identifier obtained in the previous step (for example, if FACEBOOK_APP_ID is 1595275917403961, then the added scheme should look like: fb1595275917403961 ) Details on adding URL schemes in the generated project are described in this article,but below is also a description of the addition.
Schemes for opening other applications are needed to check the availability of the official facebook application. The following schemes must be added - fbapi,fbauth, fbauth2 in LSApplicationQueriesSchemes. Details on adding URL schemes in the generated project are described in this article,but below is also a description of the addition.
Using UI:
To add schemas to LSApplicationQueriesSchemes (to open other applications), you must:
- Go to your application's Info.plist
- Add a new row called LSApplicationQueriesSchemes and the data type is array
- Add the necessary elements to the created array (
fbapi,fbauth,fbauth2)
To add a URL scheme (to open your application from the outside) you need:
- Go to your application's Info.plist
- Add the line “URL types”
- Expand the first element (“Item0”), add the string “URL identifier” and add the value with the identifier of your application (for example, com.company.appname).
- Add a line to the first element (“item0”) in “URL types” and name it “URL Schemes” and add a new element (desired URL scheme) to “URL Schemes”
The result is the following structure:

As XML:
Add the following data to your Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbauth</string>
<string>fbauth2</string>
</array>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb<FACEBOOK_APP_ID></string>
</array>
</dict>
</array>
Note: for URL schemes, if you already have such a structure in a plist with other schemes, then you can simply add another element to the scheme array (CFBundleURLSchemes or LSApplicationQueriesSchemes)
Add dependencies🔗
Add the MRGS SDK to the project:
Unity:
Adding to the project (general instruction)
Step 1. Add Sources
To add MRGS to a project via the Unity Package Manager (available from Unity 2018+) simply add a scopedRegistries section to the Packages/manifest.json file by adding the following entry:
{
"dependencies": {
...
},
"scopedRegistries": [
{
"name": "MRGS",
"url": "https://mrgs-nexus.my.games/repository/mrgs-uninty-plugins/",
"scopes": [
"games.my.mrgs"
]
}
]
}
Alternatively, you can click Edit -> Project Settings -> Package Manager -> '+' in scoped registry section, and fill in the fields according to the data above.
Step 2. Add dependency
- Click
Window -> Package Manager -> select 'Packages: MyRegistries' from dropdown list, select packageMRGSAuthenticationFacebookfrom the list, then click "Install" - Import the module:
using MRGS;
- Download the latest version of the library. Unzip the archive.
- (For unitypackage integration) In Unity, click
Assets -> Import Package -> Custom Package, and select thegames.my.mrgs.authenticationfacebook.unitypackagepackage from the downloaded archive. - (For tgz integration) In Unity, click
Window -> Package Manager -> '+' -> Add package from tarball, and select thegames.my.mrgs.authenticationfacebook-<version> package. tgzfrom the downloaded archive. - Import the module:
using MRGS;
iOS:
Adding to the project (general instruction)
Step 1: Add dependencies
Via Package collection
- In Xcode select File > Add Packages
- Select "+" > "Add Swift Package Collection"
- Insert URL:
https://mrgs-nexus.my.games/repository/ios-sdks/MRGSPackageCollection.json - Select module MRGSAuthenticationFacebook from "MRGS Package Collection".
- Or you can select "MRGS" package from "MRGS Package Collection" (contains all mrgs modules as products) and then select "MRGS/AuthenticationFacebook" product only.
Individual packages
- In Xcode select File > Add Packages
- In the search bar at the top right, paste the URL:
https://mrgs-gitea.my.games/mrgs/mrgsauthenticationfacebook-ios-sdk.git - Add a module to your project
- Or you can paste the url
https://mrgs-gitea.my.games/mrgs/ios-sdks.gitto include the "MRGS" package which contains all mrgs modules as products and then select only the product" MRGS/AuthenticationFacebook".
Step 2: Add support for ObjectiveC categories
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationFacebook;or#import <MRGSAuthenticationFacebook/MRGSAuthenticationFacebook.h>
Step 1. Add Sources
In your podfile, add sources to the top of the file:
source 'https://github.com/CocoaPods/Specs.git' # For main repo
source 'https://mrgs-gitea.my.games/mrgs/cocoapods-specs.git' # For MRGS repo
Step 2: Add dependencies
Add the latest version of MRGSAuthenticationFacebook to target:
To add via subspecs:
To add via individual modules:
To add all mrgs modules:
Step 3: Install dependencies
- Run
pod install(orpod install --repo-updateif necessary) - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationFacebook;or#import <MRGSAuthenticationFacebook/MRGSAuthenticationFacebook.h>
Step 1: Add dependencies
Add the dependency to your Cartfile:
binary "https://mrgs-nexus.my.games/repository/ios-sdks/MRGSAuthenticationFacebook/MRGSAuthenticationFacebook.json" ~> 5.0.0
Step 2: Install dependencies
- Run
carthage update --use-xcframeworks - Add downloaded frameworks to your project (make sure "do not embed" option is enabled)
- Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGServiceKit;or@import MRGSAuthenticationFacebook;or#import <MRGSAuthenticationFacebook/MRGSAuthenticationFacebook.h>
- Download the latest version of the library. Unzip the archive.
-
Add
MRGSAuthenticationFacebook.xcframeworkfrom the downloaded archive to your project (Drag the libraries to the "Linked frameworks and Libraries" section) (Also contains MRGSAuthenticationFacebook.framework for compatibility - fat framework) -
Set the
-ObjCflag in the "Other linker Flags" field in the project settings. - Import the module in code:
@import MRGSAuthenticationFacebook;or#import <MRGSAuthenticationFacebook/MRGSAuthenticationFacebook.h> - Also, you can add the
MRGServiceKit.handmodule.modulemapfiles from the archive to your project, or specify the path to them in the project settings in theBuild Settings -> Header search pathssection. Now, instead of importing each of our frameworks separately, you can only import one header file:@import MRGServiceKit;
Adding to Android project
- Add dependencies to your Application gradle file.
Setting MRGS parameters🔗
Pass received FacebookAppID value to MRGS SDK. To do this, when starting the MRGS SDK, add the appropriate setting in the external SDK parameters:
using MRGS;
public class MasterController : MonoBehaviour
{
void Awake()
{
// Setting up MRGS params
// ...
var modulesParams = new List<MRGSExternalSDKSettings>
modulesParams.Add(new MRGSFacebookParams("<FACEBOOK_APPID>"));
// Setting up external sdk params
// ...
MRGService.Instance.Initialize(serviceParams, sdkParams);
}
}
@import MRGService;
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Configure MRGS Settings
// ...
//Configure external SDKs
MRGSFacebookParams* facebookLoginParams = [[MRGSFacebookParams alloc] initWithAppId:@"<FACEBOOK_APP_ID>"];
NSArray *externalParams = @[ ..., facebookLoginParams];
[MRGService startWithServiceParams:<params> externalSDKParams:externalParams delegate:nil];
}
import games.my.mrgs.MRGSExternalSDKParams;
import games.my.mrgs.MRGSExternalSDKParams.FacebookParams;
import games.my.mrgs.MRGService;
import games.my.mrgs.MRGServiceParams;
public class YourApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
// Setting MRGService
final MRGServiceParams serviceParams = ...;
// Setting External SDKs
final MRGSExternalSDKParams externalSDKParams = MRGSExternalSDKParams.newInstance();
externalSDKParams.facebookParams = FacebookParams.init("<FACEBOOK_APP_ID>");
MRGService.service(context, serviceParams, externalSDKParams);
}
}
Facebook App Suffix
Also, if you have one facebook application for several "physical" applications (that is, if you use one facebook app id for several different applications), then you can use appSuffix to differentiate them. The corresponding field is present in MRGSFacebookParams.
Adding a button🔗
Then, Add the 'Login with Facebook' button to the login screen, in accordance with Facebook button guidelines.
Working with the MRGSAuthentication interface🔗
To work with Login with Facebook, use the MRGSAuthenticationFacebook class. Please note that this class implements main interface MRGSAuthentication, so it includes all the methods of this interface. The following describes the standard implementation of the MRGSAuthentication interface:
Delegate🔗
Before you start working with the API, you must install and implement a delegate that accepts callbacks:
// Setting
// 'this' conforms to 'IMRGSAuthenticationDelegate' protocol
MRGSAuthenticationFacebook.Instance.Delegate = this;
// Implementation
// The method that is called when the user has logged out of the social network remotely, or his session has ended.
public void OnAuthenticationProviderDidLogoutUser(MRGSAuthentication provider, MRGSAuthenticationUser user){
Debug.Log("MRGSAuthentication<Unity> - OnAuthenticationProviderDidLogoutUser: " + user + "\nNetwork: " + provider.SocialId().ToString());
}
// Setting
[MRGSAuthenticationFacebook sharedInstance].delegate = self;
// Implementation
// The method that is called when the user remotely logs out of the social network, or his session ends.
- (void)authenticationProvider:(id<MRGSAuthentication>)provider didLogoutUser:(MRGSAuthenticationUser *)user{
NSLog(@"User did logout: \n%@", user);
}
// The method that is called when the controller needs to be displayed to authorize the user.
- (void)shouldPresentAuthorizationController:(UIViewController *)viewController {
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:viewController animated:YES completion:nil];
}
import androidx.annotation.NonNull;
import games.my.mrgs.authentication.MRGSAuthentication.OnExternalLogoutListener;
import games.my.mrgs.authentication.MRGSAuthenticationNetwork;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
// Set a listener
MRGSFacebook.getInstance().setOnExternalLogoutListener(new OnExternalLogoutListener() {
@Override
public void onUserLogout(@NonNull MRGSAuthenticationNetwork network) {
// Handel result.
}
});
Status🔗
Then, you need to check the authorization status (whether the user is logged in):
Login🔗
For regular login, use one of the methods:
// If error occured, error object will be non-null
MRGSAuthenticationFacebook.Instance.Login((MRGSError error) => {
if (error == null) {
// Auth success
}
});
// The response will include an object describing the user and the token, and an error object, if any.
MRGSAuthenticationFacebook.Instance.Login((MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationFacebook:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
// Only the object of the error will come in the answer, if it happened
[[MRGSAuthenticationFacebook sharedInstance] login:^(NSError *error) {
if (!error) {
// Auth success
}
}];
// The response will contain an object describing the user and the token, and the object of the error if it happened
[[MRGSAuthenticationFacebook sharedInstance] loginWithCompletionHandler:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if (!error) {
// Auth success
}
}];
import android.app.Activity;
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSCredentials;
import games.my.mrgs.authentication.MRGSLoginCallback;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
MRGSFacebook.getInstance().login(activity, new MRGSLoginCallback() {
@Override
public void onSuccess(@NonNull MRGSCredentials credentials) {
// Handle result.
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
Error when there is no browser on Android and its handling
Facebook does not support authorization through the native WebView, only through the browser. Therefore, if the device does not have a browser installed, the callback will receive an error with the code - 110 and the description Browser not found. In this case, we recommend that you show users an alert that you need to install a browser to log in to Facebook.
Login with additional permissions🔗
You can also request additional data from the user:
- Access to friends list
- Access to the user’s description "about yourself"
- Access to the user's birthday
- Access to user gender
- Access to user's hometown
- Access to the user's location (indicated in the profile)
Description of enum:
* Email - MRGSAuthenticationFacebook.Scopes.Email
* Access Friends List - MRGSAuthenticationFacebook.Scopes.UserFriends
* Access to the user's birthday - MRGSAuthenticationFacebook.Scopes.UserBirthday
* Access to user gender - MRGSAuthenticationFacebook.Scopes.UserGender
* Access to user's hometown -MRGSAuthenticationFacebook.Scopes.UserHometown
* Access to the user's location (indicated in the profile) - MRGSAuthenticationFacebook.Scopes.UserLocation
* Email - kMRGSAuthenticationFacebookScopeEmail
* Access Friends List - kMRGSAuthenticationFacebookScopeFriends
* Access to the user’s description "about yourself" - kMRGSAuthenticationFacebookScopeUserAboutMe
* Access to the user's birthday - kMRGSAuthenticationFacebookScopeBirthday
* Access to user gender - kMRGSAuthenticationFacebookScopeGender
* Access to user's hometown - kMRGSAuthenticationFacebookScopeHometown
* Access to the user's location (indicated in the profile) - kMRGSAuthenticationFacebookScopeLocation
* Email - "email"
* Access to friends list - "user_friends"
* Access to the user’s description "about yourself" - "public_profile"
* Access to the user's birthday - "user_birthday"
* Access to user gender - "user_gender"
* Access to the user's hometown - "user_hometown"
* Access to the user's location (indicated in the profile) - "user_location"
To do this, use the method below, passing in it an array of additional permissions that you want to receive:
var scopes = new List<string>();
scopes.Add(MRGSAuthenticationFacebook.Scopes.Email);
scopes.Add(MRGSAuthenticationFacebook.Scopes.UserFriends);
scopes.Add(MRGSAuthenticationFacebook.Scopes.UserBirthday);
scopes.Add(MRGSAuthenticationFacebook.Scopes.UserGender);
scopes.Add(MRGSAuthenticationFacebook.Scopes.UserHometown);
scopes.Add(MRGSAuthenticationFacebook.Scopes.UserLocation);
MRGSAuthenticationFacebook.Instance.Login(scopes, (MRGSAuthenticationCredential credentials, MRGSError error) => {
Debug.Log("MRGSAuthenticationFacebook:\nCredentials: " + credentials + "\nError:\n" + error);
if (error == null) {
// Auth success
}
});
NSArray* scopesToRequest = @[kMRGSAuthenticationFacebookScopeEmail,kMRGSAuthenticationFacebookScopeFriends,kMRGSAuthenticationFacebookScopeUserAboutMe,kMRGSAuthenticationFacebookScopeBirthday,kMRGSAuthenticationFacebookScopeGender,kMRGSAuthenticationFacebookScopeHometown,kMRGSAuthenticationFacebookScopeLocation];
[[MRGSAuthenticationFacebook sharedInstance] loginWithScopes:scopesToRequest completionHandler:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
}];
import android.app.Activity;
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.List;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSCredentials;
import games.my.mrgs.authentication.MRGSLoginCallback;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
final List<String> scopes = Arrays.asList("email", "user_friends", "user_birthday");
MRGSFacebook.getInstance().login(activity, scopes, new MRGSLoginCallback() {
@Override
public void onSuccess(@NonNull MRGSCredentials credentials) {
// Handle result
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
Important
Note that the user may not give some permissions, selectively. The list of received permissions can be find in the credentials.accessToken.authorizedScopes field. If you call the login method even after authorization, but with an expanded list of permissions (new ones added), a window for requesting permissions from the user will be displayed.
Authorization Status🔗
To get all the information about the current authorization status and user information, use the methods:
// The information about the user, identifier, name, email, etc.
MRGSAuthenticationFacebook.Instance.GetCurrentUser((MRGSAuthenticationUser user, MRGSError error) => {
Debug.Log("MRGSAuthenticationFacebook:\User: " + user + "\nError:\n" + error);
if (error == null) {
// Work with user data
}
});
// Information about the token, lifetime, etc.
MRGSAuthenticationFacebook.Instance.GetAccessToken((MRGSAuthenticationAccessToken token, MRGSError error) => {
Debug.Log("MRGSAuthenticationFacebook:\Token: " + token + "\nError:\n" + error);
if (error == null) {
// Work with token info
}
else if (error.Code == (int)MRGSAuthenticationErrorCode.ConnectionFailed)
{
// No network, try later
}
else
{
// Logout user from app (MRGS will logout automatically in SDK)
}
});
// An object is returned that combines information about the user (identifier, name, email, etc.) and about the token.
[[MRGSAuthenticationFacebook sharedInstance] getCredentials:^(MRGSAuthenticationCredential *credentials, NSError *error) {
NSLog(@"Result - %@. Error - %@", credentials, error);
if(!error){
// Credentials data work
}
}];
// User information - identifier, name, email, etc.
[[MRGSAuthenticationFacebook sharedInstance] getCurrentUser:^(MRGSAuthenticationUser *user, NSError *error) {
NSLog(@"User - %@. Error - %@", user, error);
if(!error){
// User data work
}
}];
// Information about the token - lifetime, etc.
[[MRGSAuthenticationFacebook sharedInstance] getAccessToken:^(MRGSAuthenticationAccessToken *token, NSError *error) {
if (!error) {
NSLog(@"getAccessToken result - %@", token);
} else {
NSLog(@"getAccessToken error - %@", error);
}
}];
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAccessToken;
import games.my.mrgs.authentication.MRGSAuthentication;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
import games.my.mrgs.utils.optional.BiConsumer;
// Returns user's information.
MRGSFacebook.getInstance().getCurrentUser(new MRGSAuthentication.UserCallback() {
@Override
public void onSuccess(@NonNull final MRGSUser user) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
// Returns authentication's information.
MRGSFacebook.getInstance().getAccessToken(new BiConsumer<MRGSAccessToken, MRGSError>() {
@Override
public void accept(@Nullable MRGSAccessToken accessToken, @Nullable MRGSError error) {
if (error != null) {
Log.d("MRGSAuthentication", "AccessToken error: " + error.getErrorText());
} else {
Log.d("MRGSAuthentication", "AccessToken success: " + accessToken);
}
}
});
Logout🔗
In order to "log out" from a user’s account, use the method:
Additional🔗
Please note that if you requested additional permissions from the user to access additional fields of his profile, we will request them automatically, and the data will appear either in the corresponding fields of the object of the MRGSAuthenticationUser class, or in the optionalParams field of this object.
Working with the MRGSAuthenticationSocialNetwork interface🔗
MRGSAuthenticationSocialNetwork - is an interface of social which allows to receive the user's friends, his profile picture, make game requests and invitations to the game, and also open the "share" page.
Getting an avatar🔗
To get a user avatar with a given identifier and size, use the method:
import android.graphics.Bitmap;
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAvatarCallback;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
// Call #getCurrentUser() to get current user.
final MRGSUser user = ...
// Get user's avatar.
MRGSFacebook.getInstance().getUserAvatar(user, new MRGSAvatarCallback() {
@Override
public void onSuccess(@NonNull Bitmap bitmap) {
// Handle result.
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error.
}
});
List of friends and getting users🔗
In order to get a list of all friends of the current user, use the method:
import androidx.annotation.NonNull;
import java.util.List;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSSocial;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
MRGSFacebook.getInstance().getFriends(new MRGSSocial.FriendsCallback() {
@Override
public void onSuccess(@NonNull List<MRGSUser> users) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
Important
Please note that facebook does not return all the user's friends, but only those who installed your application. Also, for this method to work, permission to the friends list is required (specified in the login method). If there is no permission and this method is called, the user will be shown an authorization dialog with additional permission to view friends.
In order to get an object with a user description with a given identifier, use the method:
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSAuthentication;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
MRGSFacebook.getInstance().getUserWithId("userId", new MRGSAuthentication.UserCallback() {
@Override
public void onSuccess(@NonNull MRGSUser user) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
In order to get objects with a description of users with the given identifiers, use the method:
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.List;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSSocial;
import games.my.mrgs.authentication.MRGSUser;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
MRGSFacebook.getInstance().getUsersWithId(Arrays.asList("user_id_1", "user_id_2"), new MRGSSocial.FriendsCallback() {
@Override
public void onSuccess(@NonNull List<MRGSUser> users) {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
Creating a publication🔗
In order to open the dialog for creating a new post in the current user’s feed with the specified parameters, use the method:
MRGSAuthenticationPost *post = [[MRGSAuthenticationPost alloc] init];
post.caption = @"Check out MRGS site!";
post.link = @"http://mrgs.astrum.team";
[[MRGSAuthenticationFacebook sharedInstance] showPostOnWallDialog:post completionHandler:^(NSError *error) {
if(!error){
// Operation completed successfully
}
}];
Important
A dialog will open for creating a new post with the filled data specified in MRGSAuthenticationPost. For Facebook, only the caption and link fields are used in the MRGSAuthenticationPost object.
Sending invitations and game requests🔗
To send invitations to the game and game requests (using them you can send "gifts" in some cases), as well as check incoming requests, the methods below are provided. The methods accept an MRGSAuthenticationInvite object, which indicates the invitation text, a list of users to be invited, and other request parameters (see the fields of the MRGSAuthenticationInvite class). The user will be shown a dialog for confirmation. In the response, if successful, an MRGSAuthenticationInvite object will be returned with filled in fields, such as request identifier, sent users.
// Sending an invitation to join a game
var inviteIn = new MRGSAuthenticationInvite();
inviteIn.Message = @"Test invite message";
inviteIn.Title = @"Test title";
MRGSAuthenticationFacebook.Instance.SendInvite(inviteIn, (MRGSAuthenticationInvite inviteOut, MRGSError error) => {
if(error == null){
// Operation completed successfully
Debug.Log("Sended invite - " + inviteOut);
}
});
// Sending a game request
var inviteIn = new MRGSAuthenticationInvite();
inviteIn.Message = @"Test invite message";
inviteIn.Title = @"Test MRGS title";
inviteIn.Data = @"Test invite data";
inviteIn.Frictionless = true;
MRGSAuthenticationFacebook.Instance.SendGameRequest(inviteIn, (MRGSAuthenticationInvite inviteOut, MRGSError error) => {
if(error == null){
// Operation completed successfully
Debug.Log("Sended game request - " + inviteOut);
}
});
// Receiving incoming game requests to the user
MRGSAuthenticationFacebook.Instance.GetGameRequests((List<Dictionary<string, object>> requests, MRGSError error) => {
if(error == null){
// Operation completed successfully
string requestsString = requests != null ? MRGS.JSON.Json.Serialize(requests.ConvertAll(x => (object)x), true) : "null";
Debug.Log("Received requests - " + requestsString);
}
});
// Sending an invitation to join a game
MRGSAuthenticationInvite *invite = [[MRGSAuthenticationInvite alloc] init];
invite.message = @"Test invite message";
invite.title = @"Test title";
[[MRGSAuthenticationFacebook sharedInstance] sendInvite:invite completionHandler:^(MRGSAuthenticationInvite* inviteOut,NSError *error) {
if(!error){
// Operation completed successfully
NSLog(@"Sended invite - %@", inviteOut);
}
}];
// Sending a game request
MRGSAuthenticationInvite *invite = [[MRGSAuthenticationInvite alloc] init];
invite.message = @"Test invite message";
invite.title = @"Test MRGS title";
invite.data = @"Test invite data";
invite.frictionless = YES;
[[MRGSAuthenticationFacebook sharedInstance] sendGameRequest:invite completionHandler:^(MRGSAuthenticationInvite* inviteOut, NSError *error) {
if(!error){
// Operation completed successfully
NSLog(@"Sended request - %@", inviteOut);
}
}];
// Receiving incoming game requests to the user
[[MRGSAuthenticationFacebook sharedInstance] getGameRequests:^(NSArray<NSDictionary *> *requests, NSError *error) {
if(!error){
// Operation completed successfully
NSLog(@"Received requests - %@", requests);
}
}];
import androidx.annotation.NonNull;
import java.util.Collections;
import java.util.List;
import games.my.mrgs.MRGSError;
import games.my.mrgs.MRGSList;
import games.my.mrgs.authentication.MRGSSocial;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
// Send invite request
MRGSFacebook.getInstance().sendGameRequest("Game Request",
"Invite message",
true,
Collections.singletonList(friend),
new MRGSSocial.GameRequestResultCallback() {
@Override
public void onSuccess(@NonNull List<String> users) {
// Handle result
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error
}
});
// Send game request
MRGSFacebook.getInstance().sendGameRequest("Game Request",
"Game request message",
false,
Collections.singletonList(friend),
new MRGSSocial.GameRequestResultCallback() {
@Override
public void onSuccess(@NonNull List<String> users) {
// Handle result
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error
}
});
// Get games requests
MRGSFacebook.getInstance().getGameRequests(new MRGSSocial.GameRequestCallback() {
@Override
public void onSuccess(@NonNull final MRGSList requests) {
// Handle result
}
@Override
public void onError(@NonNull MRGSError error) {
// Handle error
}
});
Sending invitations or game requests
A window will open for Facebook where the user selects friends to invite.
Dictionary structure when querying incoming game queries
List of fields is available on facebook site - https://developers.facebook.com/docs/graph-api/reference/app-request/
Additional Facebook Features🔗
Also, MRGSAuthenticationFacebook offers several additional features exclusively for working with Facebook:
Since Facebook must manually delete incoming game requests, the method is pre-prepared for this (the request identifier can be obtained using the getGameRequests method):
import androidx.annotation.NonNull;
import games.my.mrgs.MRGSError;
import games.my.mrgs.authentication.MRGSSocial;
import games.my.mrgs.authentication.facebook.MRGSFacebook;
MRGSFacebook.getInstance().deleteGameRequest(itemId, new MRGSSocial.GameRequestDeleteCallback() {
@Override
public void onSuccess() {
}
@Override
public void onError(@NonNull MRGSError error) {
}
});
Created: 2020-05-28