React Native Google Play Games Services (react-native-play-games) is a wrapper around the native Google Play Games Services API for Android.
This documentation assumes that you've already completed all the requirements of the setup guide found here.
It is important that you follow and complete all the listed requirements or the library will not work.
- Create a value resource file named id.xml in the res/values folder
- Paste your application id in the created file. See example.
<string name="app_id">YOUR_APP_ID</string>
- Paste the following in your within the
<application>
tag in the AndroidManifest.xml file
...
<application>
...
+ <meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id"/>
+ <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
</application>
...
Note: if you open android studio at this point, you may get an error saying that it cannot resolve @integer/google_play_services_version. This error should be resolved after you have completed the installation and linking steps below.
-
Install the package from npm
[sudo] npm install react-native-play-games --save
OR yarn[sudo] yarn install react-native-play-games
-
Link the library
react-native link react-native-play-games
-
Edit top-level build.gradle (Project module)
allprojects {
...
repositories {
...
// Add these lines of code to your android/build.gradle file
+ maven {
+ url "https://maven.google.com"
+ }
...
}
...
}
Module | Method | Return | Since |
---|---|---|---|
RNPlayGamesAuth | onAuthStateChanged | *event handler* | 1.0.0 |
RNPlayGamesAuth | signInPlayerInBackground | Promise | 1.0.0 |
RNPlayGamesAuth | signInPlayerWithUI | Promise | 1.0.0 |
RNPlayGamesAuth | signOutPlayer | Promise | 1.0.0 |
RNPlayGamesPlayer | getCurrentPlayerInfo | Promise(playerInfoObj) | 1.0.0 |
RNPlayGamesLeaderboard | showAllLeaderboardsUI | Promise | 1.0.0 |
RNPlayGamesLeaderboard | showLeaderboardUI | Promise | 1.0.0 |
RNPlayGamesLeaderboard | showLeaderboardUIFilteredTimeSpan | Promise | 1.0.0 |
RNPlayGamesLeaderboard | submitScore | Promise({ isNewBest }) | 1.0.0 |
RNPlayGamesAchievement | showAchievementsUI | Promise | 1.0.0 |
RNPlayGamesAchievement | incrementAchievement | Promise({ isUnlocked }) | 1.0.0 |
RNPlayGamesAchievement | unlockAchievement | Promise | 1.0.0 |
RNPlayGamesAchievement | revealHiddenAchievement | Promise | 1.0.0 |
To use methods in the auth module, import RNPlayGamesAuth:
import { RNPlayGamesAuth } from 'react-native-play-games'
This method triggers the callback function whenever the user's authentication state has changed (signed in or signed out).
Important: It is important to implement this method because the sign in and sign out methods do not return the user's authentication state.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
callback | function(isSignedIn) | - | Yes | Param: isSignedIn (boolean) - whether or not the user is signed in. |
Return *event handler*
Example
...
componentWillMount() {
this.authListener = RNPlayGamesAuth.onAuthStateChanged(isSignedIn => {
this.setState({
isSignedIn: isSignedIn
});
})
}
componentWillUnmount() {
// Remove listener
if (this.authListener != null) {
this.authListener.remove();
}
}
...
Attempts to silently sign in the player/user in the background. If triggerUI is true, then if the background sign in failed, then the signin UI will be presented to the user.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
triggerUI | boolean | - | Yes | Whether or not to display the sign-in UI in the event that the silent sign in failed. |
Return
Promise<null>
Returns a promise that is fulfilled after the sign-in flow is is complete or dismissed by the user. Triggers callback provided to the onAuthStateChanged method.
Example
RNPlayGamesAuth.signInPlayerInBackground(false).catch(err => {
console.log(err);
})
Displays the interactive sign in UI to the user.
Return
Promise<null>
Returns a promise that is fullfilled after the sign-in flow is is complete or dismissed by the user. Triggers callback provided to the onAuthStateChanged(callback) method.
Example
RNPlayGamesAuth.signInPlayerWithUI().catch(err => {
console.log(err);
})
Attemps to sign out the current player/user.
Return
Promise<null>
Returns a promise that is fullfilled after the sign-out process is complete. Triggers callback provided to the onAuthStateChanged(callback) method.
Example
RNPlayGamesAuth.signOutPlayer().catch(err => {
console.log(err);
})
To use methods in the auth module, import RNPlayGamesPlayer:
import { RNPlayGamesPlayer } from 'react-native-play-games'
This method returns information about the user that is currently login in.
Return
Promise(playerInfoObj)
Name | Type | Description |
---|---|---|
playerInfoObj | object | { title, lastTimePlayed, playerId, displayName, levelInfo } |
To use methods in the auth module, import RNPlayGamesLeaderboard:
import { RNPlayGamesLeaderboard } from 'react-native-play-games'
Displays a leaderboard UI overlay containing all the leaderboards associated with the application/game.
Return
Promise<null>
Returns a promise that is fullfilled when the user exits the UI. Triggers callback provided to the onAuthStateChanged(callback) method if the user signs out in the UI.
Example
RNPlayGamesLeaderboard.showAllLeaderboardsUI().catch(err => {
alert("Failed to load UI.");
console.log(err);
})
Displays the leaderboards UI overlay to the user.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
boardId | string | - | Yes | The id of the specific leaderboard |
Return
Promise<null>
Returns a promise that is fullfilled when the user exits the UI. Triggers callback provided to the onAuthStateChanged(callback) method if the user signs out in the UI.
Example
RNPlayGamesLeaderboard.showLeaderboardUI('board-id-here').catch(err => {
alert("Failed to load UI.");
console.log(err);
})
Displays the leaderboards UI overlay to the user.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
boardId | string | - | Yes | The id of the specific leaderboard |
timeSpan | RNPlayGamesConstants | - | Yes | Use one of the provided constants: TIME_SPAN_DAILY, TIME_SPAN_WEEKLY, TIME_SPAN_ALL_TIME |
Return
Promise<null>
Returns a promise that is fullfilled when the user exits the UI. Triggers callback provided to the onAuthStateChanged(callback) method if the user signs out in the UI.
Example
RNPlayGamesLeaderboard.showLeaderboardUIFilteredTimeSpan('board-id-here', RNPlayGamesConstants.TIME_SPAN_WEEKLY).catch(err => {
console.log(err);
})
Submits the specified score for the current user to the specified leaderboard (boardId).
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
boardId | string | - | Yes | The id of the specific leaderboard |
score | integer | - | Yes | The score |
scoreTag | string | - | no (nullable) | Pass 'null' to ignore. See google's documentation for more details on scoreTags. |
Return
Promise({ isNewBest })
Name | Type | Description |
---|---|---|
isNewBest | boolean | whether or not the score is a new best |
Example
RNPlayGamesLeaderboard.submitScore('board-id-here', 200, null).then(mess => {
if(mess.isNewBest) {
alert("You set a new best score.");
}
}).catch(err => {
console.log(err);
})
To use methods in the achievements module, import RNPlayGamesAchievement:
import { RNPlayGamesAchievement } from 'react-native-play-games'
Displays all the achievements associated with the application/game.
Return
Promise<null>
Example
RNPlayGamesAchievement.showAchievementsUI().catch(err => {
console.log(err);
})
Increments the specific achievement by the specified number of steps for the current player/user.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
id | string | - | Yes | The ID of the achievement to increment. |
numSteps | integer | - | Yes | The number of steps to increment by. Must be greater than 0. |
Return |
Promise({ isUnlocked })
Name | Type | Description |
---|---|---|
isUnlocked | boolean | indicates whether the achievement is now unlocked |
Example
RNPlayGamesAchievement.incrementAchievement('achievement-id-here', 2).then(mess => {
if(mess.isUnlocked) {
alert("New Achievement unlocked!");
}
}).catch(err => {
console.log(err);
})
Unlocks an achievement for the currently signed in player. If the achievement is hidden this will reveal it to the player.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
id | string | - | Yes | The ID of the achievement to unlock. |
Return
Promise<null>
Example
RNPlayGamesAchievement.unlockAchievement('achievement-id-here').catch(err => {
console.log(err);
})
revealHiddenAchievement(id)
Reveals a hidden achievement to the currently signed-in player. If the achievement has already been unlocked, this will have no effect.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
id | string | - | Yes | The ID of the hidden achievement to reveal. |
Return
Promise<null>
Example
RNPlayGamesAchievement.revealHiddenAchievement('achievement-id-here').catch(err => {
console.log(err);
})