List Notifications and Mark as Read In-Game
In this How-to, we’ll get all unread notifications for the logged in player, find a specific notification by a key and mark it as read.
Prerequisites
A LootLocker account and a created game
A trigger or a catalog item setup in the web console
A previously invoked trigger or a successful purchase
To get the matched notification to a previous action (an invoked trigger, a purchase call), you need one or more of the following:
A purchase using virtual currency: use the
catalog_listing_id
An In App Purchase using Apple App Store: use the
transaction_id
An In App Purchase using Google Pla Store: use the
product_id
An In App Purchase using Steam Store: use the
entitlement_id
A trigger: use the
trigger_key
// We're only interested in unread notifications for this how to.
// If you want to see information about notifications you've previously marked as read then set this to true
bool showRead = false;
// The key that we saved from invoking a trigger, or calling a purchase to get the correct notification
string notificationKey = "saved_trigger_key_or_catalog_item_id_from_a_previous_response";
// You can optionally return only a specific priority
LootLocker.LootLockerEnums.LootLockerNotificationPriority? priority = null;
// You can optionally return only a specific type of notification.
// Triggers and purchased items are of the type LootLocker.LootLockerStaticStrings.LootLockerNotificationTypes.PullRewardAcquired
string ofType = "";
// The source of the notification: a type of purchase or trigger.
// Use the static strings provided in LootLocker.LootLockerStaticStrings.LootLockerNotificationSources
// to specify what type of notification you are requesting.
// For purchases, use:
// LootLocker.LootLockerStaticStrings.LootLockerNotificationSources.Purchasing.(the source of purchase: LootLocker, AppleAppStore, GooglePlayStore, SteamStore)
// For triggers, use:
// LootLocker.LootLockerStaticStrings.LootLockerNotificationSources.Triggers
string notificationSource = LootLocker.LootLockerStaticStrings.LootLockerNotificationSources;
// The Page and Count parameters are used for pagination.
// Count means count per page, and Page is what page to access.
// For example: A count of 100, and page 2 would list all notifications (if any) from 101-201
int count = 10;
int page = 1;
LootLockerSDKManager.ListNotifications(showRead, priority, LootLockerNotificationTypes.PullRewardAcquired, notificationSource, count, page, (response) =>
{
if (!response.success)
{
Debug.Log("Error, could not list notifications:" + response.errorData.message);
// Add your own code here to handle the error
return;
}
// A notifications request returns success even if no notifications were found,
// handle that by checking if the notifications returned are null
if (response.Notifications == null)
{
Debug.LogWarning("Error: no notifications found");
// Add your own code to handle what should happen
return;
}
// Get the desired notification
LootLockerNotification[] matchingNotifications;
bool notificationWasFound = response.TryGetNotificationsByIdentifyingValue(notificationKey, out matchingNotifications);
if ( !notificationWasFound || matchingNotifications?.Length == 0 )
{
Debug.LogWarning("Error: Notification not found.");
// Add your own code here to handle the error
return;
}
// In this example we know that the trigger has only been invoked once so we can safely access the first element of the array
LootLockerNotification desiredNotification = matchingNotifications[0];
if (desiredNotification == null)
{
Debug.LogWarning("Error: Notification not found.");
// Add your own code here to handle the error
return;
}
// The enum value in the notification.Content.Body.Kind field shows what type of "thing" was rewarded, use that to know which field in the body contains the data about the reward.
switch (desiredNotification.Content.Body.Kind)
{
case LootLocker.LootLockerEnums.LootLockerNotificationContentKind.group:
// The reward is a reward group which contains a list of "associations". Ie, a list of assets, currencies, or other similar things rewarded as one in this single reward.
// Handle this in line with your game logic
Debug.Log($"Trigger with key '{notificationKey}' gave reward of type group. The group has name '{desiredNotification.Content.Body.Group.Name}', description '{desiredNotification.Content.Body.Group.Description}'. and {desiredNotification.Content.Body.Group.Associations.Length} associations.");
break;
case LootLocker.LootLockerEnums.LootLockerNotificationContentKind.currency:
// The reward is a currency. Handle this in line with your game logic
Debug.Log($"Trigger with key '{notificationKey}' gave reward of type currency: {desiredNotification.Content.Body.Currency.Details.Amount} {desiredNotification.Content.Body.Currency.Details.Code}");
break;
case LootLocker.LootLockerEnums.LootLockerNotificationContentKind.asset:
// The reward is an asset. Handle this in line with your game logic
Debug.Log($"Trigger with key '{notificationKey}' gave reward of type asset with name '{desiredNotification.Content.Body.Asset.Details.Name}'");
break;
case LootLocker.LootLockerEnums.LootLockerNotificationContentKind.progression_reset:
// The reward is a progression reset. Handle this in line with your game logic
Debug.Log($"Trigger with key '{notificationKey}' gave reward of type progression reset which resets the progression named '{desiredNotification.Content.Body.Progression_reset.Details.Name}'");
break;
case LootLocker.LootLockerEnums.LootLockerNotificationContentKind.progression_points:
// The reward is progression points. Handle this in line with your game logic
Debug.Log($"Trigger with key '{notificationKey}' gave reward of type progression points which gives the player {desiredNotification.Content.Body.Progression_points.Details.Amount} points in the progression named '{desiredNotification.Content.Body.Progression_points.Details.Name}'");
break;
default:
Debug.LogWarning($"Unhandled case {desiredNotification.Content.Body.Kind.ToString()}");
break;
}
// After you've handled the notification in your game logic, remember to mark it as read.
// This way it will not be returned on the next request unless you specifically request notifications marked as read.
desiredNotification.MarkThisNotificationAsRead((response) =>
{
if (response.success)
{
Debug.Log("Marked notification as read");
}
else
{
Debug.LogWarning("Error marking notification as read: " + response.errorData.message);
// Add your own code here to handle the error
}
});
});
Input
In this example, we're filtering notifications by their source using the triggers string. Other filters include:
Show Read: Set to true to include previously read notifications.
Priority: Fetch notifications of a specific priority.
Of Type: Fetch notifications of a specific type, such as PullRewardAcquired for triggers and purchased items. If there are many notifications, use pagination to navigate through the data.
Mark as read
Once a notification is processed in your game logic, mark it as read. This will exclude it from future requests unless you specifically request read notifications. This action should be applied to each item in the list of handled notifications
.
Logic for handling Notifications
When listing notifications, branch the completed event based on the success flag and add error handling for failed requests.
Note on Pagination: The notification response includes pagination data to help you navigate large notification lists.
We recommend branching the completed event from listing notifications on the success flag, and if you do this you will probably want to add error handling in case the request fails.
Here, we retrieve only unread notifications from the triggers source with default pagination (up to 10 items). We then filter further by SuccessfullyInvokedTriggerKey
to narrow the list to specific triggers.
In this example, we simply print the notification info. Adapt this step to handle notifications based on your game’s requirements. Here, we assume that SuccessfullyInvokedTriggerKey
provides a currency reward and only expand that information.
For a more general approach, iterate over all notifications to handle various sources and rewards relevant to your game.
curl --location --request GET 'https://api.lootlocker.io/game/notifications/v1?per_page=10&page=1' \
--header 'x-session-token: 3266edd928f5769545425469ac417fee456d8367' \
curl --location --request PUT 'https://api.lootlocker.io/game/notifications/v1/read' \
--header 'x-session-token: 3266edd928f5769545425469ac417fee456d8367' \
--header 'Content-Type: application/json' \
--data-raw '{
"notifications": [
"01J6VYWY115186HY5N87HQDEC0",
"01J6VYWY0S1Z0MKKP9MTA8WZKB",
"01J6VYWY0F1MJ6E7QS93PZ4TQ7"
]
}'
It is important to mark notifications as read after you have taken action on it, otherwise you will need to go through all notifications each time, which increases response times, resulting in a bad experience for your players.
Conclusion
In this How-to, we’ve listed a notification and marked it as read. Notifications are to be called after a purchase has been made or after an invoked trigger.
Last updated