2

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.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Naveen
  • 769
  • 1
  • 9
  • 28
  • Possible duplicate of [Can't create handler inside thread that has not called Looper.prepare()](https://stackoverflow.com/questions/3875184/cant-create-handler-inside-thread-that-has-not-called-looper-prepare) – Zoe Jan 14 '19 at 15:34
  • Not a duplicate, please read the question again @Zoe – Naveen Jan 14 '19 at 15:41
  • I think @Zoe is correct and the solution from stackoverflow question he mentioned can be used to solve your issue. It is not a total duplicate, but the solution seem same. Please, try that solution and report if it works for you. – Anhayt Ananun Jan 14 '19 at 17:37
  • 1
    That solution didnt work @Anhayt, i asked this after browsing stackoverflow – Naveen Jan 15 '19 at 06:58
  • I see. What error do you get when you try it? Also, can you add the code for your attempt to the question? – Anhayt Ananun Jan 15 '19 at 19:49

1 Answers1

0

Finally i got the solution myself, the FirebaseMessagingService() does not have a context to inflate the view, so i have to start a service inside the onMessageReceived method and inflate the view from there like

val fcmDrawOverlayService = Intent(this@FCMMessageReceiverService, FcmDrawOverlayService::class.java)
startService(fcmDrawOverlayService)

and inflate it on fcmDrawOverlayService onCreate() method.

Naveen
  • 769
  • 1
  • 9
  • 28