I'm working on an App that gets data via Firebase messaging, inserts it into SQLite and displays information in the App (if it's visible)
First I have the Firebase service setup in MANIFEST.XML
<!-- [START firebase_service] -->
<service
android:name=".MyHandler">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
Next in MyHandler.java I handle a new message event and call the insert in SQLite helper
public class MyHandler extends FirebaseMessagingService {
SQLActivity dbsrv = new SQLActivity(this);
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
dbsrv.crtMSG("SQ32 ETA 11:25");
sendNotification("Flight update");
if (MainActivity.isVisible) {
MainActivity.mainActivity.SnackNotify("Flight update");
}
This is the relevant call in my SQLIte helper class
public class SQLActivity extends SQLiteOpenHelper{
public void crtMSG(String sMSG) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MSGWRXF.MSGH.MSG, sMSG);
db.insert("MSGH", null, values);
db.close();
}
Occasionally I get this error, usually it seems when multiple messages are received simultaneously:
"Exception java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed."
Not entirely sure of the Firebase messaging architecture, but it seems like messaging events are firing off inserts so quickly that they don't all complete before the connection gets closed.
This App works absolutely fine whether the App is in foreground, background and closed state when messages arrive one at a time! It also works fine even if the device is switched when it is sent messages, they arrive when it's switched on again
I'm wondering if maybe I could just drop the db.close() in the database insert..?