I know it's been a while but I finally figured out how to get the WhatsApp notification image.
I decompiled an app that could intercept the images (it's on the Playstore) and I managed to find out how it did it.
First get the URI of the image:
private fun getUriFromNotification(notification: Notification): Uri? {
val parcelableArray = notification.extras.getParcelableArray(m.EXTRA_MESSAGES)
return parcelableArray?.let { getUriFromParcelableArray(it) }
}
private fun getUriFromParcelableArray(parcelableArr: Array<Parcelable>): Uri? {
if (parcelableArr.isEmpty()) return null
val lastParcelable = parcelableArr.last()
return if (lastParcelable is Bundle) {
lastParcelable.getParcelable("uri")
} else {
null
}
}
After that convert to your taste (mine is base64)
fun getFileAsBase64(context: Context, uri: Uri): String? {
val contentResolver: ContentResolver = context.contentResolver
val inputStream: InputStream? = contentResolver.openInputStream(uri)
inputStream?.let {
// Converte InputStream para Base64
val byteArrayOutputStream = ByteArrayOutputStream()
val buffer = ByteArray(1024)
var bytesRead: Int
while (it.read(buffer).also { bytesRead = it } != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead)
}
val byteArray = byteArrayOutputStream.toByteArray()
// Converte para Base64
return Base64.encodeToString(byteArray, Base64.NO_WRAP)
}
return null
}