Notifications - Code

Allow game makers to message players, regardless if game is running [LIVE-Notifications-02]

Overview

The Notifications feature can be accessed via the NotificationsService from the Beamable API. Notifications primarily function using a Publish-subscribe pattern, where objects that should react to incoming messages (the "subscribers") listen for messages being sent out by another object (the "publishers"). Following this pattern, the API is quite simple.

Usages

Example Callback Function

When a subscribed object receives a message, the message carries some data with it that can provide extra content about the event that occurred. For the purposes of this document, this function will be used:

void CallbackAction(object callbackObject)
{
    //Some code that uses the callback object
}

Subscribe/Unsubscribe

Subscriptions are PlayerId based, so an event published by the server (e.g., a microservice) must specify your PlayerId in order for your client to receive the event.

var beamable = BeamContext.Default;

//Start listening to events published with the specified message name
beamable.Api.NotificationService.Subscribe("messageName", CallbackAction );

//Stop listening for events with the specified message name
beamable.Api.NotificationService.Unsubscribe("messageName", CallbackAction );

Publish (from Microservice)

public async Task SendNotificationFromMicroservice(List<long> playerIds, string context, object payload)
{
    var json = JsonUtility.ToJson(payload);
    await Services.Notifications.NotifyPlayer(playerIds, context, json);
}