I am trying out Firebase Notifications. I was able to get the Notification to work properly using this documentation. The message was received and I was able to send a notifica
To save data received on onMessgeReceived() to SQLite database even when your app is in the background (no activity running), you can do the following:
1) Create a class that extends IntentService, e.g:
public class SQLService extends IntentService {
private final static String MESSAGE_ID = "message_id";
private MySQLiteDbAdapter mySQLiteAdapter;
public SQLService() {
super("test-service");
}
@Override
public void onCreate() {
super.onCreate();
// initialize SQLite adapter here using getApplicationContext()
this.mySQLiteAdapter = new MySQLiteDbAdapter(getApplicationContext());
}
@Override
protected void onHandleIntent(Intent intent) {
// fetch data to save from intent
Message message = new Message();
Message.setMessage_id(intent.getStringExtra(MESSAGE_ID));
...
// save
this.mySQLiteAdapter.add(message);
}
}
2) Launch the service class from onMessageReceived() method or a method within your extension of the Firebase service, e.g.:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData() != null) {
Intent intent = new Intent(this, SQLService.class);
// add data to intent
intent.putExtra(MESSAGE_ID, remoteMessage.getData().get(MESSAGE_ID));
...
// start the service
startService(intent);
}
}
3) Register the service in your AndroidManifest.xml:
...
For more in-depth explanation see https://guides.codepath.com/android/Starting-Background-Services