I am using fcm for push notifications, whenever a push notification arrives i need to show a view to user from the service(even when the app is killed or in background), i tried to do it like facebook messenger style by drawing overlays, i have already got the permission for making overlays (android.permission.SYSTEM_ALERT_WINDOW), this is my service class
package com.radisolutions.radipeople.radihome.services
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.util.Log
import android.view.LayoutInflater
import android.view.WindowManager
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.radisolutions.radipeople.radihome.R
class FCMMessageReceiverService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
Log.i("naveen", remoteMessage?.notification?.body.toString())
val view = LayoutInflater.from(applicationContext).inflate(R.layout.overlay_visitor_alert, null)
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val layoutParams = WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT)
windowManager.addView(view, layoutParams)
}
override fun onCreate() {
super.onCreate()
}
}
but at the the line windowmanager.addview it triggers the below error
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
How to fix this? This is working normally when i create a view inside which broadcastreceiver class, but doesn't work on firebase service class.