Thursday, 27 October 2022

Fetch image in FirebaseMessagingService causing UnknownHostException

We are using image is our push notification. This is the code to get the bitmap from URL:

try {
        return BitmapFactory.decodeStream(URL(url).content as InputStream)
    } catch (e: Exception) {
        //Used for debugging production data only
        if (BuildConfig.FLAVOR.equals("production", true)
                && BuildConfig.BUILD_TYPE.equals("release", true)) {
            if (logData != null)
                FirebaseCrashlytics.getInstance().setCustomKey("Push Notification Test Payload", logData?.toString()
                        ?: "")
            FirebaseCrashlytics.getInstance().recordException(e)
        }
    }

Earlier it was being used directly but since it's a long running task so I moved it inside CoroutineScope to avoid NetworkOnMainThreadException as below:

CoroutineScope(Dispatchers.IO).launch {
            val bitmap = getBitmap(imagePath!!)
            val largeIcon = BitmapFactory.decodeResource(this@PushMessagingService.resources,
                    R.drawable.notification_big_icon)
            withContext(Dispatchers.Main) {
                if (bitmap != null) {
                    val notificationStyle = NotificationCompat.BigPictureStyle()
                    notificationStyle.bigPicture(bitmap)
                    notificationStyle.setSummaryText(msg)
                    buildNotification(channelId, msgTitle, msg, defaultSoundUri, contentIntent,
                            notificationStyle, largeIcon, notificationManager, notificationID)
                } else {
                    val notificationStyle = NotificationCompat.BigTextStyle()
                    buildNotification(channelId, msgTitle, msg, defaultSoundUri, contentIntent,
                            notificationStyle, largeIcon, notificationManager, notificationID)
                }
            }
        }

But now I am getting lots of UnknownHostException stack trace in Crashlytics. This is stack trace:

Non-fatal Exception: java.net.UnknownHostException: Unable to resolve host "cdn.test.com": No address associated with hostname
   at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:156)
   at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
   at java.net.InetAddress.getAllByName(InetAddress.java:1152)
   at com.android.okhttp.Dns$1.lookup(Dns.java:41)
   at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
   at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
   at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
   at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
   at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
   at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
   at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
   at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
   at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
   at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
   at com.google.firebase.perf.network.InstrURLConnectionBase.getContent(InstrURLConnectionBase.java:86)
   at com.google.firebase.perf.network.InstrHttpsURLConnection.getContent(InstrHttpsURLConnection.java:68)
   at com.google.firebase.perf.network.FirebasePerfUrlConnection.getContent(FirebasePerfUrlConnection.java:124)
   at com.google.firebase.perf.network.FirebasePerfUrlConnection.getContent(FirebasePerfUrlConnection.java:92)
   at app.push.PushMessagingService.getBitmap(PushMessagingService.kt:254)
   at app.push.PushMessagingService.access$getBitmap(PushMessagingService.kt:35)
   at app.push.PushMessagingService$sendNotification$1.invokeSuspend(PushMessagingService.kt:145)
   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
   at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
   at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
   at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
   at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
   
   Caused by android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
   at libcore.io.Linux.android_getaddrinfo(Linux.java)
   at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:73)
   at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:202)
   at libcore.io.ForwardingOs.android_getaddrinfo(ForwardingOs.java:73)
   at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:135)
   at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
   at java.net.InetAddress.getAllByName(InetAddress.java:1152)
   at com.android.okhttp.Dns$1.lookup(Dns.java:41)
   at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
   at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
   at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
   at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
   at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
   at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
   at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
   at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
   at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
   at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:106)
   at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:30)
   at com.google.firebase.perf.network.InstrURLConnectionBase.getContent(InstrURLConnectionBase.java:86)
   at com.google.firebase.perf.network.InstrHttpsURLConnection.getContent(InstrHttpsURLConnection.java:68)
   at com.google.firebase.perf.network.FirebasePerfUrlConnection.getContent(FirebasePerfUrlConnection.java:124)
   at com.google.firebase.perf.network.FirebasePerfUrlConnection.getContent(FirebasePerfUrlConnection.java:92)
   at app.push.push.PushMessagingService.getBitmap(PushMessagingService.kt:254)
   at app.push.push.PushMessagingService.access$getBitmap(PushMessagingService.kt:35)
   at app.push.push.PushMessagingService$sendNotification$1.invokeSuspend(PushMessagingService.kt:145)
   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
   at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
   at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
   at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
   at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
   at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)

I got the server checked and its working fine. That raised me some concerns:

  1. Is anything wrong in my implementation with respect to push notifications? Since, I am not facing any issue anywhere in rest of app.
  2. What would be the efficient way to fetch bitmap from url in case of push notifications?


from Fetch image in FirebaseMessagingService causing UnknownHostException

No comments:

Post a Comment