title: MVVM - Accessing ViewModel/SQLite in a BroadcastReceiver started from a notification when app is closed
body: <p>I have a reminder notification that is sent every few days. </p>
  
  <p>The sending of that notification is triggered with a repeating <code>AlarmManager</code>. The notification itself is built in the <code>onReceive</code> of my <code>BroadcastReceiver</code> (as described <a href="https://stackoverflow.com/a/39674974/1972372">here</a>). So when <code>onReceive</code> is triggered, the app is not even open/running.</p>
  
  <p>Now at that point <strong>I want to access my (local) SQLite database</strong> and get the correct content to build the notification with, but how would I get a <code>ViewModelProvider</code> (xxx in the code) in this place to even access my <code>ViewModel</code>?</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {    
  
      NotificationViewModel viewModel = 
      ViewModelProviders.of(XXX).get(NotificationViewModel.class);
  
      //do stuff
  }
  </code></pre>
  
  <p>Or to ask the better question, is that even good practice?<br>
  The other possibility would be stuffing all the content in the <code>PendingIntent</code> that will trigger the <code>onReceive</code>, 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.</p>
  
  <hr>
  
  <p>I've looked at a few search results but they did not solve my issue:  </p>
  
  <ul>
  <li><a href="https://medium.com/@matthias.c.siegmund/mvvm-architecture-for-custom-views-on-android-b5636cb6be26" rel="nofollow noreferrer">MVVM Architecture for Custom Views on Android</a><br>
  -> Seems like a lot of code for such a minor issue, plus being Kotlin, which is hard to understand for me</li>
  <li><a href="https://javaforteadrinkers.wordpress.com/2014/05/26/dealing-with-broadcastreceivers-using-the-model-view-presenter-design-pattern/" rel="nofollow noreferrer">Dealing with BroadcastReceivers using the Model View Presenter design pattern</a><br>
  -> 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</li>
  <li><a href="https://stackoverflow.com/questions/51490558/accessing-broadcastreceiver-in-viewmodel">Accessing BroadCastReceiver in Viewmodel</a><br>
  -> <strong>This seems like the closest to what I need</strong>, 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?<br>
  Is it even possible to access my database without my app being in the foreground? </li>
  </ul>
  
  <hr>
  
  <h2>Edit:</h2>
  
  <p>Reading <a href="https://medium.com/androiddevelopers/livedata-beyond-the-viewmodel-reactive-patterns-using-transformations-and-mediatorlivedata-fda520ba00b7" rel="nofollow noreferrer">LiveData beyond the ViewModel [...]</a>, it is said  </p>
  
  <blockquote>
    <p>If part of your app doesn’t affect the UI, you probably don’t need LiveData.</p>
  </blockquote>
  
  <p>So that means I should simply access my repository with the context and get the raw data out of it, <strong>without the LiveData wrapper?</strong>  </p>
  
  <p>So</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
  
      NotificationRepository rp = new NotificationRepository(context);
      MessageNotification notification = rp.getNextNotification();
  }
  </code></pre>
  
  <p>instead of  </p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
  
      NotificationViewModel viewModel = 
      ViewModelProviders.of(XXX).get(NotificationViewModel.class);
      MessageNotification notification = 
      viewModel.getNextNotification().observe(XXX, new 
           Observer&lt;MessageNotification&gt;() {
              @Override
              public void onChanged(MessageNotification messageNotification) {
                  //do stuff
              }
           });
  }
  </code></pre>
  
  <p>But does this go against the MVVM convention?<br>
  Should I use some other architecture? Right now it seems to make sense to me, since <strong>it's something I only retrieve once and don't have to observe for changes.</strong></p>