Home > Mobile >  WorkManager throws NetworkOnMainThreadException when in background
WorkManager throws NetworkOnMainThreadException when in background

Time:01-03

I want my app to read emails and send notifications while in the background while using workmanager, my work manager is stopping working after a couple of executions throwing NetworkOnMainThreadException on background tasks: I want to make a reliable app for sending notifications for newly received emails which means I need it to execute forever every 15 minutes. Do you know how can I avoid this problem?

WorkManager class:

class WorkerMan(private val mContext: Context, workerParameters: WorkerParameters) :
    Worker(mContext, workerParameters) {


    @SuppressLint("RestrictedApi", "CheckResult")
    val email = inputData.getString("email")
    val password = inputData.getString("password")

    @SuppressLint("RestrictedApi")
    override fun doWork(): Result {
        println("WorkManager: Work called")
        println("WorkMananager time: "   LocalTime.now())
        try {



       // Thread.sleep(5000)


        val session = Session.getDefaultInstance(Properties())
        val store = session.getStore("imaps")
        store.connect(
            "mail.metropolitan.ac.rs",
            993,
            email,
            password
        )
        val inbox = store.getFolder("INBOX")
        inbox.open(Folder.READ_ONLY)


        val messages = inbox.search(
            FlagTerm(Flags(Flags.Flag.SEEN), false)
        )


        Arrays.sort(
            messages
        ) { m1: Message, m2: Message ->
            try {
                return@sort m2.sentDate.compareTo(m1.sentDate)
            } catch (e: MessagingException) {
                throw RuntimeException(e)
            }
        }





        Thread.sleep(1000)
        println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ")
        println("WorkManager Started")
        println("WorkMananager email: "   email)
        val current = LocalTime.now()
        println("WorkMananager time: "   current)
        println("Messages amount: "   messages.size)
        println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ")

        for (message in messages) {


//            if (message.receivedDate.toInstant() >= Instant.now().minusMillis(1000 * 33 * 60)) {
                if (true) {

               // Thread.sleep(3000)
                println("=====================================================")
                println("NOTIFIKACIJA")

                var title = ""
                for (element in message.from) {
                    title  = element.toString().substringAfter("<").substringBefore(">")
                    title  = " "
                }
                println("Title :"   title)
                println("Subject :"   message.subject)
                println("Datum i vreme : "   message.receivedDate)

                title.replace("[", "")
                title.replace("]", "")



                FirebaseMessaging.getInstance().token.addOnSuccessListener { token: String ->
                    if (!TextUtils.isEmpty(token)) {
                        Log.d("TAG", "retrieve token successful : $token")
                        try {
                            send(token, message.subject, title)
                        } catch (e: MessagingException) {
                            e.printStackTrace()
                        }
                    } else {
                        Log.w("TAG", "token should not be null...")
                    }
                }.addOnFailureListener { e: Exception? -> }.addOnCanceledListener {}
                    .addOnCompleteListener { task: Task<String> ->
                        Log.v(
                            "TAG",
                            "This is the token : "   task.result
                        )
                    }


//
            }


        }
        println("=====================================================")
        Log.d("WorkManager", "Job finished")
        }catch (e : Exception){
            Log.d("WorkManager error", "doWork not executed")
            Log.d("WorkManager error", "error: ")
            Log.d("WorkManager error", e.printStackTrace().toString())
        }
        return Result.Success();
    }

}


fun send(to: String?, body: String?, title: String?): String? {
    try {
        val apiKey =
            "AAAA_Xfga4Q:APA91bH1cASekbIF7zSiqVkOtEJdQX2-qLO6yJp_iiTzYetdy6pRBl-uq28a27sdzDUvAUI51XO7IQaiKk_eccW0fTeFj8-4z7236mzoFaniTPMYR4Xmhzn5RYkAh-ON3tXKnELu7IEC"
        val url = URL("https://fcm.googleapis.com/fcm/send")
        val conn = url.openConnection() as HttpURLConnection
        conn.doOutput = true
        conn.requestMethod = "POST"
        conn.setRequestProperty("Content-Type", "application/json")
        conn.setRequestProperty("Authorization", "key=$apiKey")
        conn.doOutput = true
        val message = JSONObject()
        message.put("to", to)
        message.put("priority", "high")
        val notification = JSONObject()
        notification.put("title", title)
        notification.put("body", body)
        message.put("notification", notification)
        val os = conn.outputStream
        os.write(message.toString().toByteArray())
        os.flush()
        os.close()
        val responseCode = conn.responseCode
        println("\nSending 'POST' request to URL : $url")
        println("Post parameters : $message")
        println("Response Code : $responseCode")
        println("Response Code : "   conn.responseMessage)
        val `in` = BufferedReader(InputStreamReader(conn.inputStream))
        var inputLine: String?
        val response = StringBuffer()
        while (`in`.readLine().also { inputLine = it } != null) {
            response.append(inputLine)
        }
        `in`.close()

        // print result
        println(response.toString())
        return response.toString()
    } catch (e: Exception) {
        Log.d("WorkManager error", "send not executed")
        Log.d("WorkManager error", "error: ")
        Log.d("WorkManager error", e.printStackTrace().toString())
    }
    return "error"
}

Function that schedule workmanager:

 @SuppressLint("RestrictedApi")
    private fun startWorker(email: String, password: String) {

        if (enableMailNotifications == 1) {


//            val constraints = Constraints.Builder()
//                .setRequiredNetworkType(NetworkType.CONNECTED).build()

            val data = Data.Builder()
            data.put("email", email)
            data.put("password", password)


            val build: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
                WorkerMan::class.java,
                15,
                TimeUnit.MINUTES,
                15,
                TimeUnit.MINUTES,

                )

                .addTag("WorkManager")
                .setInputData(data.build())
                //  .setConstraints(constraints)
                .build()

            WorkManager.getInstance(this)
                .enqueueUniquePeriodicWork("WorkManager", ExistingPeriodicWorkPolicy.REPLACE, build)
        }else{
            println("WorkManager not called: Mail Notifications are turned off")
        }
    }

EDIT: Full StackTraces:

pastebin.com/BVxyA4uF pastebin.com/ujYtNu60

CodePudding user response:

I fixed a problem by calling

FirebaseMessaging.getInstance().token.addOnSuccessListener

In MainActivity and passing token as an inputData for WorkManager.

data.put("token", token)

And retrieving it in WorkManager class by:

val token = inputData.getString("token")
  • Related