title: Should I use coroutine in onMessageReceived() of FirebaseMessagingService?
body: <p>I am developing an Android application using MVVM design pattern.</p>
  <p>I have a class <code>FCMService</code> that extends <code>FirebaseMessagingService</code>.</p>
  <p>As you may know, the <code>FCMService</code> overrides <code>onMessageReceived(remoteMessage: RemoteMessage)</code> function.</p>
  <p>So, whenever I receive a message in <code>onMessageReceived()</code> function, I want to save it to room database via a repository.</p>
  <p>It looks like the code below.</p>
  <pre><code>class FCMService : FirebaseMessagingService(), KodeinAware {
  
      override val kodein by closestKodein()
      private val repository: Repository by instance()
      private val scope: CoroutineScope by instance()
  
      override fun onNewToken(token: String) {
      }
  
      override fun onMessageReceived(remoteMessage: RemoteMessage) {
          super.onMessageReceived(remoteMessage)
          CoroutineScope(Dispatchers.IO).lauch{ repository.save(remoteMessage) }
      }
  }
  </code></pre>
  <pre><code>class Repository {
     suspend fun save(remoteMessage: RemoteMessage) {
        withContext(Dispatchers.IO) {
           someDAO.save(removeMessage)
        }
     }
  }
  </code></pre>
  <p>I read a stackoverflow <a href="https://stackoverflow.com/questions/41067081/does-firebasemessagingservice-run-in-the-background-by-default">post</a> and found out that the <code>onMessageReceived()</code> function executes in background thread and all work that is done within onMessageReceived(RemoteMessage message) should be done synchronously.</p>
  <p>So, here are my questions please</p>
  <ol>
  <li><p>Should I use <code>CoroutineScope(Dispatchers.IO).lauch {}</code> in <code>onMessageRecevied()</code> function?</p>
  </li>
  <li><p>If no, then I can just use normal function, not suspend function in repository and I can call it from <code>onMessageReceived()</code> without <code>CoroutineScope(Dispatchers.IO).launch {}</code>. Is it correct in terms of architectural design point of view please?</p>
  </li>
  <li><p>It's a question about Coroutine but, as you can see that I launched a new coroutine in IO thread by <code>CoroutineScope(Dispatchers.IO).lauch{ repository.save(remoteMessage) }</code> in <code>FCMService</code> but I also switch the coroutineContext from IO to IO by <code>withContext(Dispatchers.IO) { someDAO.save(removeMessage) }</code> in <code>Repository</code>. I feel that <code>withContext(Dispatchers.IO) { someDAO.save(removeMessage) }</code> is unnecessary because I am switching from IO to IO. Am I right please?</p>
  </li>
  </ol>