Use case sample: Send a push message to the player whose score was beaten
Send a push message to a player whose leaderboard score was beaten by another player.
Read time 5 minutesLast updated 18 hours ago
The use case sample demonstrates how to use Triggers to send a push message to a player whose score was beaten in a leaderboard. The sample uses the
score-submittedPrerequisites
You must first create a service account with required access roles and configure the UGS CLI.Authenticate using a Service Account
Before you can call the Triggers service, you must authenticate using a Service Account.- Navigate to the Unity Dashboard.
- Select Administration > Service Accounts.
- Select the New button and enter a name and description for the Service Account.
- Select Create.
- Select Manage product roles.
- Add the following roles to the Service Account:
- From the LiveOps dropdown, select Triggers Configuration Editor, Triggers Configuration Viewer, and Leaderboards Admin.
- From the Admin dropdown, select Unity Environments Viewer.
- From the LiveOps dropdown, select Triggers Configuration Editor, Triggers Configuration Viewer, and Leaderboards Admin.
- Select Save.
- Select Add Key.
- Encode the Key ID and Secret key using base64 encoding. The format is “key_id:secret_key”. Note this value down.
Configure the UGS CLI
Follow the steps below to get stated with the UGS CLI:- Install the UGS CLI.
-
Configure your Project ID and Environment as such:
ugs config set project-id <your-project-id>
ugs config set environment-name <your-environment-name> - Authenticate using the Service account you created earlier. Refer to Get Authenticated.
Set up Leaderboards
To follow this sample, you should create a leaderboard. You can use the UGS CLI to deploy the followingleaderboard.lbnew-fileThe file name corresponds to the leaderboard ID. Update theugs leaderboards new-file leaderboard
leaderboard.lbDeploy the file using the UGS CLI tool:{ "$schema": "https://ugs-config-schemas.unity3d.com/v1/leaderboards.schema.json", "SortOrder": "asc", "UpdateType": "keepBest", "Name": "leaderboard"}
Now that you have created a leaderboard, you can add scores to it.ugs deploy leaderboard.lb
Optional: Add scores to the leaderboard
You can run the following Cloud Code script to add scores to the leaderboard from the Unity Dashboard. Make sure to select the Generate icon to regenerate the Player ID token on every test run to generate a score for a new player.JavaScript
const { LeaderboardsApi } = require("@unity-services/leaderboards-1.1");const _ = require("lodash-4.17");module.exports = async ({ params, context, logger }) => { const leaderboardsApi = new LeaderboardsApi({ accessToken: context.accessToken }); const leaderboardID = "leaderboard"; var randomScore = _.random(1, 100); try { await leaderboardsApi.addLeaderboardPlayerScore(context.projectId, leaderboardID, context.playerId, { score: randomScore }); } catch (err) { logger.error("Failed to add score to the leaderboard", { "error.message": err.message }); }};
Examine the score-submitted
event
The Leaderboards service emits a score-submittedscore-submittedThe event passes the event payload to Cloud Code as parameters. Refer to Leaderboards: Score Submitted for more information.{ "leaderboardId": "leaderboard", "updatedTime": "2019-08-24T14:15:22Z", "playerId": "5drhidte8XgD4658j2eHtSljIAzd", "playerName": "Jane Doe", "rank": 42, "score": 120.3, "tier": "gold"}
Set up Cloud Code
Define a module endpoint that sends a push message to the player whose score was beaten. Create aSendPushNotificationDeploy the module. Refer to Deploying Hello World to learn how to deploy a module.using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Logging;using Unity.Services.CloudCode.Apis;using Unity.Services.CloudCode.Core;using Unity.Services.CloudCode.Shared;namespace LeaderboardsNotification;public class LeaderboardsNotification{ private readonly ILogger<LeaderboardsNotification> _logger; public LeaderboardsNotification(ILogger<LeaderboardsNotification> logger) { _logger = logger; } [CloudCodeFunction("SendPlayerMessage")] public async Task SendPlayerMessage(IExecutionContext context, IGameApiClient gameApiClient, PushClient pushClient, string playerId, string playerName, string leaderboardId, double score) { try { var closestPlayers = await gameApiClient.Leaderboards.GetLeaderboardScoresPlayerRangeAsync(context, context.ServiceToken, new Guid(context.ProjectId), leaderboardId, playerId, 1); if (closestPlayers.Data.Results.Count != 0) { var player = closestPlayers.Data.Results[^1]; string message = $"The player {playerName} has just beaten your score of {player.Score} on the {leaderboardId} leaderboard by {score-player.Score} points!"; await pushClient.SendPlayerMessageAsync(context, message, "Information", player.PlayerId); } } catch (ApiException e) { _logger.LogError("Failed to send push notification to player {playerId}. Error: {Error}", playerId, e.Message); throw new Exception($"Failed to send push notification to player {playerId}. Error: {e.Message}"); } }}public class ModuleConfig : ICloudCodeSetup{ public void Setup(ICloudCodeConfig config) { config.Dependencies.AddSingleton(PushClient.Create()); config.Dependencies.AddSingleton(GameApiClient.Create()); }}
Configure a trigger
To connect your Cloud Code resource to the Leaderboardsscore-submittednew-fileUpdate theugs triggers new-file triggers-config
triggers-config.trDeploy the configuration using the UGS CLI tool:{ "$schema": "https://ugs-config-schemas.unity3d.com/v1/triggers.schema.json", "Configs": [ { "Name": "score-beaten-message", "EventType": "com.unity.services.leaderboards.score-submitted.v1", "ActionUrn": "urn:ugs:cloud-code:LeaderboardsNotification/SendPlayerMessage", "ActionType": "cloud-code" } ]}
A successful response is similar to the following:ugs deploy triggers-config.tr
The sample trigger executes your Cloud Code module function when a player signs up.Deployed:triggers-config.tr
Validate the result
To validate the result, you can set up a Unity project that subscribes to push messages and use the Cloud Code script you created earlier to submit a new score to the leaderboard.Prerequisites
To subscribe to push messages, you need to install the Cloud Code SDK and link your Unity Gaming Services project to the Unity Editor.Link project
Link your Unity Gaming Services project with the Unity Editor. You can find your UGS project ID in the Unity Dashboard.- In Unity Editor, select Edit > Project Settings > Services.
-
Link your project.
If your project doesn't have a Unity project ID:- Select Create a Unity Project ID > Organizations, then select an organization from the dropdown.
- Select Create project ID.
If you have an existing Unity project ID:- Select Use an existing Unity project ID.
- Select an organization and a project from the dropdowns.
- Select Link project ID.
UnityEditor.CloudProjectSettings.projectIdSDK installation
To install the latest Cloud Code package for Unity Editor:- In the Unity Editor, open Window > Package Manager.
- In the Package Manager, select the Unity Registry list view.
- Search for , or locate the Cloud Code package in the list.
com.unity.services.cloudcode - Select the package, then select Install.
Create a Monobehaviour
script
To subscribe to player-level messages, set up a MonobehaviourMonobehaviourMonoBehaviourThe first time you run theusing System;using System.Threading.Tasks;using Newtonsoft.Json;using Unity.Services.Authentication;using Unity.Services.CloudCode;using Unity.Services.CloudCode.Subscriptions;using Unity.Services.Core;using UnityEngine;namespace CloudCode{ public class CloudCodePushExample : MonoBehaviour { async void Start() { await UnityServices.InitializeAsync(); await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log(AuthenticationService.Instance.PlayerId); await SubscribeToPlayerMessages(); } // This method creates a subscription to player messages and logs out the messages received, // the state changes of the connection, when the player is kicked and when an error occurs. Task SubscribeToPlayerMessages() { // Register callbacks, which are triggered when a player message is received var callbacks = new SubscriptionEventCallbacks(); callbacks.MessageReceived += @event => { Debug.Log(DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK")); Debug.Log($"Got player subscription Message: {JsonConvert.SerializeObject(@event, Formatting.Indented)}"); }; callbacks.ConnectionStateChanged += @event => { Debug.Log($"Got player subscription ConnectionStateChanged: {JsonConvert.SerializeObject(@event, Formatting.Indented)}"); }; callbacks.Kicked += () => { Debug.Log($"Got player subscription Kicked"); }; callbacks.Error += @event => { Debug.Log($"Got player subscription Error: {JsonConvert.SerializeObject(@event, Formatting.Indented)}"); }; return CloudCodeService.Instance.SubscribeToPlayerMessagesAsync(callbacks); } }}
MonobehaviourGot player subscription Message:{ "data_base64": <BASE64-ENCODED-DATA>, "time": "2023-09-19T10:26:40.436878688Z", "message": "The player AdjacentMowingToga#7 has just beaten your score of 120.3 on the my-leaderboard leaderboard by 51.3 points!", "specversion": "1.0", "id": <ID>, "source": "https://cloud-code.service.api.unity.com", "type": "com.unity.services.cloud-code.push.v1", "projectid": <PROJECT-ID>, "environmentid": <ENVIRONMENT-ID>, "correlationid": <CORRELATION-ID>, "messagetype": "Information",}