I have a reminder notification that is sent every few days.
The sending of that notification is triggered with a repeating AlarmManager
. The notification itself is built in the onReceive
of my BroadcastReceiver
(as described here). So when onReceive
is triggered, the app is not even open/running.
Now at that point I want to access my (local) SQLite database and get the correct content to build the notification with, but how would I get a ViewModelProvider
(xxx in the code) in this place to even access my ViewModel
?
public void onReceive(Context context, Intent intent) {
NotificationViewModel viewModel =
ViewModelProviders.of(XXX).get(NotificationViewModel.class);
//do stuff
}
Or to ask the better question, is that even good practice?
The other possibility would be stuffing all the content in the PendingIntent
that will trigger the onReceive
, so I could retrieve it one by one once received. But that would be even harder, since it's a repeating alarm and needs different content every time but is triggered only once.
I've looked at a few search results but they did not solve my issue:
- MVVM Architecture for Custom Views on Android
-> Seems like a lot of code for such a minor issue, plus being Kotlin, which is hard to understand for me - Dealing with BroadcastReceivers using the Model View Presenter design pattern
-> Here it seems like it is necessary to set up a few things in the activities first, before even being able to use it, but I need to start this code without having my application running - Accessing BroadCastReceiver in Viewmodel
-> This seems like the closest to what I need, but it also needs previous setting up before. I wanted to try this but I would need to manually create my MainActivity to access its variables like that. Wouldn't that open a new activity on the user's device without warning?
Is it even possible to access my database without my app being in the foreground?
Edit:
Reading LiveData beyond the ViewModel [...], it is said
If part of your app doesn’t affect the UI, you probably don’t need LiveData.
So that means I should simply access my repository with the context and get the raw data out of it, without the LiveData wrapper?
So
public void onReceive(Context context, Intent intent) {
NotificationRepository rp = new NotificationRepository(context);
MessageNotification notification = rp.getNextNotification();
}
instead of
public void onReceive(Context context, Intent intent) {
NotificationViewModel viewModel =
ViewModelProviders.of(XXX).get(NotificationViewModel.class);
MessageNotification notification =
viewModel.getNextNotification().observe(XXX, new
Observer<MessageNotification>() {
@Override
public void onChanged(MessageNotification messageNotification) {
//do stuff
}
});
}
But does this go against the MVVM convention?
Should I use some other architecture? Right now it seems to make sense to me, since it's something I only retrieve once and don't have to observe for changes.