Inbox
The inbox is a mailbox inside the game for personal messages: gifts, compensations, messages to players where pushes do not work. It works on every platform, including platforms without push notifications, and does not count towards the push limits.
For tasks like informing players about game news and changes, it is better to use Channels instead of a mass send to the inbox. It uses many times fewer requests.
How It Works
- Messages get into the inbox from campaigns with the Inbox, Push and inbox or Inbox if no push channel delivery, from the server API and from chat notifications with
inbox: true. - Each message has tags. Use them to split the inbox into sections: "Gifts", "Friends", "Support".
- A message can be claimable: the player claims it, and the game gives the gift described in the
payloadof the message. Other messages are regular letters. More - Messages have a lifetime: 14 days by default, up to 90 days. After that the message disappears.
- Broadcast messages of campaigns are not copied to every player in advance: the player receives them when the inbox is requested. So a campaign to the whole audience does not wait for a mass mailing.
- Personal messages (server API, chat) are stored for each player separately.
- While the game is open, a new message comes in real time: show a toast or update the badge without requests.
- The inbox stores up to 50 latest personal messages per player, older ones are removed. Campaign messages come on top of this limit.
- In the control panel you can view, send, edit and delete the messages of a specific player and compensate a player with a gift.
List of Methods
Actions:
gp.inbox.fetch()- fetch messages. +1 Requestgp.inbox.fetchMore()- fetch more messages. +1 Requestgp.inbox.fetchUnreadCount()- fetch the number of unread messages (up to 100). +1 Requestgp.inbox.read()- mark messages as read. FREEgp.inbox.claim()- claim the gift from a message. +0-1 Requestgp.inbox.remove()- remove a message. FREEgp.inbox.open()- open the built-in inbox window. +1 Request
Events:
gp.inbox.on('fetch')- messages fetched. FREEgp.inbox.on('error:fetch')- failed to fetch messages. FREEgp.inbox.on('fetchMore')- more messages loaded. FREEgp.inbox.on('error:fetchMore')- failed to load more messages. FREEgp.inbox.on('fetchUnreadCount')- number of unread messages fetched. FREEgp.inbox.on('error:fetchUnreadCount')- failed to fetch the number of unread messages. FREEgp.inbox.on('event:message')- a new message came while the game is open. FREEgp.inbox.on('read')- messages marked as read. FREEgp.inbox.on('error:read')- failed to mark messages as read. FREEgp.inbox.on('claim')- message claimed. FREEgp.inbox.on('error:claim')- failed to claim a message. FREEgp.inbox.on('remove')- message removed. FREEgp.inbox.on('error:remove')- failed to remove a message. FREEgp.inbox.on('open')- the inbox window opened. FREEgp.inbox.on('close')- the inbox window closed. FREE
Open the Inbox Window
+1 RequestThe fastest way to add an inbox to the game is the built-in window. It shows the messages, loads more on scroll and marks them as read.
// All messages
gp.inbox.open();
// Only messages with the "gifts" tag and your own window title
gp.inbox.open({ tags: ['gifts'], title: 'Gifts' });
Claimable messages have the Claim button. The window claims the message with lazy: true and then calls gp.player.sync() itself. So it is enough to give the gift synchronously in the claim handler: the gift and the claim are saved with one request.
// giveGift - your function that gives the gift by payload, see "Claim a Gift"
gp.inbox.on('claim', ({ message }) => giveGift(message));
The claim event also fires when the game calls gp.inbox.claim() itself. Give the gift in one place, either in the handler or after your claim call, otherwise the player gets it twice.
Unread Counters
+1 RequestThe number of unread messages is a separate request: the SDK does not fetch the inbox by itself, call it when you need a badge. The server counts at most 100 messages — show "99+" above 99.
// Total unread messages
const count = await gp.inbox.fetchUnreadCount();
// Unread messages with the "gifts" tag
const gifts = await gp.inbox.fetchUnreadCount({ tags: ['gifts'] });
let unread = 0;
function renderInboxBadge(count) {
myGame.inboxButton.setBadge(count > 99 ? '99+' : count);
}
async function refreshInboxBadge() {
unread = await gp.inbox.fetchUnreadCount();
renderInboxBadge(unread);
}
// When the main menu opens and after the player closes the inbox window
refreshInboxBadge();
gp.inbox.on('close', refreshInboxBadge);
A new message while the game is open comes in event:message, so the badge can be updated without a request.
New Message in Real Time
FREEThe game learns about a new message right away, without requests: show a toast or light up the inbox button while the player is playing. The handler gets the same message object as the fetch() items, so a gift can be claimed with gp.inbox.claim() at once.
gp.inbox.on('event:message', (message) => {
myGame.showToast(message.title);
// +1 on the badge without a request, see "Unread Counters"
unread += 1;
renderInboxBadge(unread);
});
The "Allow players to connect to online service" option must be enabled in the Channels section of your project.
Personal messages come right away. Messages of mass campaigns come in real time only to players who are in the game when the campaign starts and match its audience. A scheduled campaign with the local time option comes in real time only to players whose local time has already come. The other players see the message in the next gp.inbox.fetch().
Fetch Messages
+1 RequestIf you want to draw the inbox yourself, fetch the first page of messages:
const result = await gp.inbox.fetch({
// Only messages with these tags, optional
tags: ['gifts'],
// Only unread messages, optional
unread: true,
// Page size, 20 by default, up to 100
limit: 20,
});
// Messages
result.items;
// More messages can be loaded: gp.inbox.fetchMore()
result.canLoadMore;
The response has no unread count — use gp.inbox.fetchUnreadCount() for a badge.
Fetch More
+1 RequestThe next page of the list. Pass the same filter (tags, unread) as in fetch(): the SDK continues the list for the filter you pass. If this filter has not been fetched yet, you get its first page.
// The first page of gifts
await gp.inbox.fetch({ tags: ['gifts'] });
// The next page of gifts - the same filter
const { items, canLoadMore } = await gp.inbox.fetchMore({ tags: ['gifts'] });
Message fields:
const message = result.items[0];
// Message ID
message.id;
// Message tags
message.tags;
// Title and text in the player's language
message.title;
message.text;
// Icon and image
message.icon;
message.image;
// Payload of the message, for example '{"gold":500}'
message.payload;
// Can the player claim the message
message.claimable;
// Is the message read
message.isRead;
// Is the message claimed
message.isClaimed;
// Creation date and expiration date, ISO 8601
message.createdAt;
message.expiresAt;
Mark as Read
FREE// By message IDs
gp.inbox.read({ ids: ['6750a1c2f1b2c3d4e5f60718'] });
// All messages with tags
gp.inbox.read({ tags: ['gifts'] });
// All messages
gp.inbox.read();
To update the badge, call gp.inbox.fetchUnreadCount() or decrease your counter by the messages read.
Claim a Gift
+0-1 RequestOnly a claimable message (message.claimable) can be claimed. claim only marks the message as claimed. The game decides what to give the player, usually by message.payload (for example, '{"gold":500}') and/or message.tags. The message must be loaded with gp.inbox.fetch() / fetchMore() or gp.inbox.open(), or come in event:message.
Agree on the payload format and give the gift in one function:
function giveGift(message) {
let gift = null;
try {
gift = JSON.parse(message.payload);
} catch (err) {}
if (gift?.gold) {
gp.player.add('gold', gift.gold);
}
}
Delayed Saving (Recommended)
FREEWith lazy: true the promise resolves right away without a request, and the message is marked as claimed locally. Give the gift and call gp.player.sync(): the player changes and the claim are saved on the server with one request. If the sync fails, neither the gift nor the claim is saved, and the next sync sends them again. It works the same way as lazy in rewards.
async function claimGift(id) {
const { message } = await gp.inbox.claim({ id, lazy: true });
giveGift(message);
// Saves the gift and the claim with one request
await gp.player.sync();
}
With Server Confirmation
+1 RequestWithout lazy the SDK syncs the player right away and resolves the promise after the server confirms the claim. After that, give the gift and save the player.
const { message } = await gp.inbox.claim({ id: '6750a1c2f1b2c3d4e5f60718' });
giveGift(message);
await gp.player.sync();
If the server response is lost, the message may end up claimed while the gift is not given. That is why we recommend lazy: true.
Repeated Claim and Errors
A message is claimed only once. The SDK checks everything it knows locally, without a request to the server: these checks do not spend requests, and repeated presses of the button are safe.
- Without
lazy, if the message was claimed earlier (including in another tab or on another device), the server reports it and the promise is rejected with theinbox_already_claimederror. Do not give anything. - With
lazy, if the message was already claimed on the server, the sync just does not mark it again. The player progress is saved as a whole by the last write, so the gift is not given twice. - Without
lazy, if the sync fails, the promise is rejected with the sync error orsync_failed. The claim is cancelled, and the player can claim the message again. - One sync saves up to 200 claims. This matters only if the game claims hundreds of messages at once.
| Error | What it means |
|---|---|
inbox_message_not_found | The message is not in the loaded list. Get the messages with gp.inbox.fetch() first |
inbox_message_not_claimable | The message is not claimable |
inbox_claim_in_progress | The same message is already being claimed and waits for the server response (repeated presses) |
inbox_already_claimed | The message is already claimed: locally, waiting for a sync with lazy, or the server reports it was claimed earlier (without lazy) |
inbox_claim_cancelled | During a profile conflict the player chose another profile, and the unconfirmed claim was cancelled. A rare case |
Remove a Message
FREEgp.inbox.remove({ id: '6750a1c2f1b2c3d4e5f60718' });
Events
// Messages fetched (fetch) and loaded more (fetchMore)
gp.inbox.on('fetch', ({ items, canLoadMore }) => {});
gp.inbox.on('fetchMore', ({ items, canLoadMore }) => {});
// Failed to fetch messages
gp.inbox.on('error:fetch', (error) => {});
gp.inbox.on('error:fetchMore', (error) => {});
// The number of unread messages fetched
gp.inbox.on('fetchUnreadCount', (count) => {});
gp.inbox.on('error:fetchUnreadCount', (error) => {});
// A new message came while the game is open, see "New Message in Real Time"
gp.inbox.on('event:message', (message) => {
myGame.showToast(message.title);
});
// Messages marked as read: { ids } | { tags } | { all }
gp.inbox.on('read', (input) => {});
gp.inbox.on('error:read', (error) => {});
// Message claimed
gp.inbox.on('claim', ({ message, lazy }) => {
myGame.showToast('Gift received!');
});
// Failed to claim a message
gp.inbox.on('error:claim', (error, { input }) => {
// error - error code, see "Repeated Claim and Errors"
// input - claim parameters: { id, lazy }
});
// Message removed
gp.inbox.on('remove', ({ id }) => {});
gp.inbox.on('error:remove', (error) => {});
// The inbox window opened and closed
gp.inbox.on('open', () => myGame.pause());
gp.inbox.on('close', () => myGame.resume());
Example: Gift Box with Tabs
const tabs = [
{ tag: 'gifts', title: 'Gifts' },
{ tag: 'friends', title: 'Friends' },
{ tag: 'support', title: 'Support' },
];
async function renderTab(tag) {
const { items } = await gp.inbox.fetch({ tags: [tag], limit: 20 });
myGame.inboxWindow.render(
items.map((message) => ({
title: message.title,
text: message.text,
isNew: !message.isRead,
canClaim: message.claimable && !message.isClaimed,
onClaim: () => claimGift(message.id),
})),
);
gp.inbox.read({ tags: [tag] });
}
Stay in Touch
Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.
GamePush Community Telegram: @gs_community.
For your suggestions e-mail: official@gamepush.com
We Wish you Success!