I want to store the firebase push notification into sqlite when the app in any state like app is closed/background/foreground. How can i do this? Is it possible to do this?
-
Are you using this in android only? – FreakyAli Mar 01 '19 at 12:00
-
yes, xamarin android – Mohanasundharam Mar 01 '19 at 12:02
-
Okay that is easy give me a min adding the code soon – FreakyAli Mar 01 '19 at 12:03
-
yes, please.... – Mohanasundharam Mar 01 '19 at 12:16
3 Answers
In your FirebaseMessagingService
inheriting class you have two important methods that you can use for this:
For Foreground notifications:
This method is called when your application is in foreground state
public override void OnMessageReceived(RemoteMessage message)
{
// SQLite saving code here
}
And For Background and Killed state notifications:
This method is called when your application is in background/killed state
public override void HandleIntent(Intent p0)
{
base.HandleIntent(p0);
// SQLite saving code here
}
Note: that in certain cases this function is also called in foreground notifications that might cause duplication.

- 13,349
- 3
- 23
- 63
-
Fine. But here how can i get notification content like "var getBody = message.GetNotification().Body;" – Mohanasundharam Mar 01 '19 at 12:38
-
If you check the parameter intent which comes in this method it has the Extras property i.e. `p0.Extras` which is an `Android.OS.Bundle` and you can get the Data in it using the `GetString` method just pass in the relevant keys which you can find when you debug this property – FreakyAli Mar 01 '19 at 12:47
-
Well basically you will have to debug this property and find your message in it – FreakyAli Mar 01 '19 at 13:12
-
Check [this](https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows#handle-notification-intents) – FreakyAli Mar 01 '19 at 13:28
-
Well, in that case, you will have to create custom notifications: https://stackoverflow.com/a/40728153/7462031 once you do this all your notifications will call the OnMessageReceived method – FreakyAli Mar 01 '19 at 13:41
-
When you are getting your notification in OnMessageReceived
method, check app state using this method
private bool isApplicationInTheBackground()
{
bool isInBackground;
RunningAppProcessInfo myProcess = new RunningAppProcessInfo();
ActivityManager.GetMyMemoryState(myProcess);
isInBackground = myProcess.Importance != Android.App.Importance.background;
return isInBackground;
}
If your app in background
state, fetch notification information from OnMessageReceived
and insert that in desired table
public override void OnMessageReceived(RemoteMessage message)
{
string orgId;
var data = message.Data.ToDictionary(i => i.Key, i => i.Value);
if (data.ContainsKey("organization"))
orgId = data["organization"];
if(isApplicationInTheBackground())
{
var db = GetCon();
db.Insert(orgId); //here should be your table.
}
}
And when you want to send/deliver saved notifications use local android push notifications.

- 13,982
- 14
- 97
- 173
-
-
How to call OnMessageReceived() when app in background & closed state? – Mohanasundharam Mar 01 '19 at 11:45
-
When ever notification delivering this method automatic getting called irrespective of app state. – R15 Mar 01 '19 at 12:27
here is the final solution for this problem, thanks to Mr.G.hakim
public override void OnMessageReceived(RemoteMessage message)
{
Log.Debug(TAG, "From: " + message.From);
Log.Debug(TAG, "Notification Message Body: " + getBody);
SendNotification(getBody, message.Data);
}
public override void HandleIntent(Intent p0)// this method will fire when the app in background and closed state
{
base.HandleIntent(p0);
if (p0.Extras != null)
{
foreach (var key in p0.Extras.KeySet())
{
var value = p0.Extras.GetString(key);
Log.Debug(TAG, "Key: {0} Value: {1}", key, value);
if(key== "gcm.notification.title")
{
Log.Debug("Delay Notification Title", "" + value);
getBGTitle = value;//declared local variable
}
else if(key== "gcm.notification.body")
{
Log.Debug("Delay Notification Body", "" + value);
getBGBody = value;//declared local variable
insertData(getBGTitle,getBGBody)//call method for store SQLite Insert
}
}
}
}

- 11
- 6