Capacitor Play Games Services is a native Google Play Games Services implementation for Android.
- To be able to use this plugin it is necessary to download it to the project with npm:
> npm i capacitor-play-games-services
- Once downloaded you have to update the project to recognize the plugin:
> npx cap update && npx cap sync
- Open Android Studio and register the plugin in your Android project.
- Go to android > app > src > main > java > domain name of your project > MainActivity.java
- Add the plugin to your MainActivity, example:
package com.xx.xx;
...
import gammafp.playgames.PlayGamesPlugin;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(PlayGamesPlugin.class);
// Important, super.onCreate(...) should go down
super.onCreate(savedInstanceState);
}
}
-
Add games id to your project:
- Go to your google play games services console (you should know this) and click on achievements or markers and below you will see the option to obtain resources:
To integrate it into your javascript project, you must import the capacitor module and then get the plugin.
import { PlayGames } from 'capacitor-play-games-services';
Listen for a change event: (this is interesting to listen the first login event)
PlayGames.addListener("onSignInStatus", (res) => {
setLoginData(JSON.stringify(res, null, 4));
})
playGames.login().then((response) => {
/* response return:
id: string;
display_name: string;
icon: string; // URI Does not work yet.
title: string;
message: string; // A messate that say what happen with the plugin correct / error (usded for tests)
isLogin: boolean; TRUE if is online FALSE if is offline
*/
});
PlayGames.status().then((response) => {
/* response return:
message: string; // A messate that say what happen with the plugin correct / error (usded for tests)
isLogin: boolean; TRUE if is online FALSE if is offline
*/
});
this method has been removed by google.
PlayGames.showAllLeaderboard();
PlayGames.showLeaderboard({
id: 'XXXXXXXX-XXXXXXXX' // id of your leaderboard
});
PlayGames.submitScore({
id: 'XXXXXXXX-XXXXXXXX' // id of your leaderboard,
score: 10 // int value only
});
Show all medals
PlayGames.showAchievements();
PlayGames.unlockAchievement({
id: 'XXXXXXXX-XXXXXXXX' // id of your achievement
});