I am developing a chat application by using ionic cordova. I am using PushNotification when user receive of new message. The problem is when user receive of new message. My application have to update the chat list in service background. However, ionic cordova cannot do service in background. How can I update the chat list in background? I have an idea to create a custom plugin once user onMessage() receive notification in PushNotification plugin, it will call another custom plugin. In the custom plugin, I am using urlconnection to call php server to get the latest information from server. Next, custom plugin is updating the sqlite information in phone. Is it a good practice to do in this way?
I suggest you to modify the Push notification Plugin code. whenever You receive the notification, in GCMIntentService.java there is a check to find if app is in foreground or background. If app is not in foreground then save the notification payload data in SharedPreferences by using following syntax
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("pushdata", data);
// Commit the edits!
editor.commit();
Whenever user opens the app check the shared preferences and get the stored data by using the following plugin.
cordova plugin add cordova-plugin-shared-preferences --save
example code to get shared preferences
document.addEventListener('deviceready', () => {
const prefs = window.plugins.SharedPreferences
prefs.getSharedPreferences('shared_preferences', 'MODE_PRIVATE', () => {
prefs.putString('pref_key', 'some text')
prefs.getString('pref_key', (value) => {
alert(value)
}, (error) => {
// handle error
})
}, (error) => {
// handle error
})
}
Hope it helps you.