I didn't see an in-game mailbox referenced in the docs, but it seems this is achievable by using Global Entities to push messages from the system via API Hooks (like a "welcome gift" that is sent after a first-time authentication, or a tournament prize after a tournament concludes), and a User Entity to track if a message was read or the Item Bundle was claimed.
It seems Custom Entities are an even better fit for this, since then, we can search for players that are, say > level 20, and push a special mail message to them, right?
Is this the most best way to approach an in-game mailbox in brainCloud, and what hurdles might I run into by handling it this way? Would this let me safely bypass the limitations around awarding Items for milestones/quests?
Here is my code for a "GetMailbox" cloud script, for reference:
"use strict";
function main() {
var entityProxy = bridge.getEntityServiceProxy(); // for user entities (tracker)
var globalProxy = bridge.getGlobalEntityServiceProxy(); // for global entities (server mail)
// fetch global mail manifest using global proxy
var globalMails =[];
var globalRes = globalProxy.getList({ "entityType": "globalMail" }, {}, 50);
if (globalRes.data.entityList) {
for (var i = 0; i < globalRes.data.entityList.length; i++) {
globalMails.push(globalRes.data.entityList[i].data);
}
}
// fetch user's personal mailbox tracker
var userRes = entityProxy.getList({ "entityType": "mailbox" }, {}, 1);
var mailboxEntity;
if (userRes.data.entityList && userRes.data.entityList.length > 0) {
mailboxEntity = userRes.data.entityList[0].data;
// safety: ensure arrays exist if entity was somehow corrupted
if (!mailboxEntity.claimedGlobalIds) mailboxEntity.claimedGlobalIds =[];
if (!mailboxEntity.personalMail) mailboxEntity.personalMail =[];
} else {
// create if it doesn't exist
mailboxEntity = { claimedGlobalIds: [], personalMail:[] };
entityProxy.createEntity("mailbox", mailboxEntity, null);
}
// merge/format for client
var mergedInbox =[];
// global mail
for (var g = 0; g < globalMails.length; g++) {
var gMail = globalMails[g];
// if msg ID is in claimed list, mark as read/claimed
gMail.isClaimed = (mailboxEntity.claimedGlobalIds.indexOf(gMail.messageId) !== -1);
gMail.isRead = gMail.isClaimed;
gMail.type = "GLOBAL";
mergedInbox.push(gMail);
}
// personal mail
for (var p = 0; p < mailboxEntity.personalMail.length; p++) {
var pMail = mailboxEntity.personalMail[p];
pMail.type = "PERSONAL";
mergedInbox.push(pMail);
}
return { messages: mergedInbox };
}
main();