-
Notifications
You must be signed in to change notification settings - Fork 7
Message storage
Mobile Messaging SDK for React Native supports a message storage feature. If the storage is enabled in configuration, then the plugin will save all push messages to the configured message storage. Plugin will handle and save messages that are received both during background and during foreground operation of the app. Two types of message storage configuration are supported: default storage and an external one.
Mobile Messaging SDK supports a built-in message storage. defaultMessageStorage
option shall be set to true in initialization configuration to enable it. If default message storage is enabled, then it will be possible to access all the messages received by the library using methods described below.
mobileMessaging.init({
applicationCode: '<your_application_code>',
defaultMessageStorage: true,
ios: {
notificationTypes: ['alert', 'badge', 'sound']
}
},
() => {
console.log('Mobile Messaging started');
},
error => {
console.log('Init error', JSON.stringify(error));
},
);
...
/**
* Retrieves all messages from message storage
*/
mobileMessaging.defaultMessageStorage().findAll(function(messages){
console.log('Currently have ' + messages.length + ' messages in default storage');
});
//**
* Retrieves message from the storage using provided message id
*/
mobileMessaging.defaultMessageStorage().find('existing-message-id', function(message) {
console.log('Found message by id: ' + JSON.stringify(message));
});
/**
* Deletes all messages
*/
mobileMessaging.defaultMessageStorage().deleteAll(function() {
console.log('Deleted all messages')
});
/**
* Deletes a messaging with the provided message id
*/
mobileMessaging.defaultMessageStorage().delete('existing-message-id', function() {
console.log('Deleted all messages')
});
Default message storage is a simple wrapper implementation over Core Data on iOS and SQLite on Android and is currently not designed to support large numbers of received messages. Note that performance of default message storage may decrease with the increasing number of messages stored inside. It is recommended to use external message storage to have full control over received messages.
Mobile Messaging SDK for React Native can be initialized with a custom external implementation of message storage. In this case the plugin will use the supplied message storage to save all the received messages. This option is recommended because in this case developer has full control over how and where messages are stored and which procedures apply. External message storage has to comply with the interface below in order to be used with Mobile Messaging SDK for React Native.
var myStorageImplementation = {
/**
* Will be called by the plugin when messages are received and it's time to save them to the storage
*
* @param {Array} array of message objects to save to storage
*/
save: function(messages) {
},
/**
* Will be called by the plugin to find a message by message id
*
* @param {Function} callback has to be called on completion with one parameter - found message object
*/
find: function(messageId, callback) {
},
/**
* Will be called by the plugin to find all messages in the storage
*
* @param {Function} callback has to be called on completion with one parameter - an array of available messages
*/
findAll: function(callback) {
},
/**
* Will be called by the plugin when its time to initialize the storage
*/
start: function() {
},
/**
* Will be called by the plugin when its time to deinitialize the storage
*/
stop: function() {
}
}
Then an external message storage has to be supplied with initialization configuration so that SDK will be able to use it to store received messages.
mobileMessaging.init({
applicationCode: '<your_application_code>',
messageStorage: myStorageImplementation,
ios: {
notificationTypes: ['alert', 'badge', 'sound']
}
},
() => {
console.log('MobileMessaging started');
},
error => {
console.log('Init error', JSON.stringify(error));
},
);
This section covers an example implementation of external message storage with the key-value AsyncStorage
implemented in @react-native-comunity/async-storage
.
import AsyncStorage from '@react-native-community/async-storage';
myMessageStorage = {
save: function(messages) {
for (const [index, message] of messages.entries()) {
AsyncStorage.setItem(message.messageId, JSON.stringify(message));
}
console.log(
'[CustomStorage] Saving messages: ' + JSON.stringify(messages),
);
},
find: async function(messageId, callback) {
console.log('[CustomStorage] Find message: ' + messageId);
let message = await AsyncStorage.getItem(messageId);
if (message) {
console.log('[CustomStorage] Found message: ' + message);
callback(JSON.parse(message));
} else {
callback({});
}
},
findAll: function(callback) {
console.log('[CustomStorage] Find all');
this.getAllMessages(values => {
console.log(
'[CustomStorage] Find all messages result: ',
values.toString(),
);
callback(values);
});
},
start: function() {
console.log('[CustomStorage] Start');
},
stop: function() {
console.log('[CustomStorage] Stop');
},
getAllMessages(callback) {
try {
AsyncStorage.getAllKeys().then(keys => {
console.log('Then AllKeys: ', keys);
AsyncStorage.multiGet(keys).then(values => {
console.log('Then AllValues: ', values);
callback(values);
});
});
} catch (error) {
console.log('[CustomStorage] Error: ', error);
}
},
};
And Mobile Messaging can be initialized to use this storage as below:
mobileMessaging.init({
applicationCode: '<your_application_code>',
messageStorage: myMessageStorage,
ios: {
notificationTypes: ['alert', 'badge', 'sound']
}
},
() => {
console.log('MobileMessaging started');
},
error => {
console.log('Init error', JSON.stringify(error));
},
);
If you have any questions or suggestions, feel free to send an email to support@infobip.com or create an issue.
- Library events
- Server errors
- Users and installations
- Messages and notifications management
- Inbox
- Privacy settings
- In‐app chat
- WebRTC Calls and UI
- Migration guides