@@ -15,6 +15,8 @@ import com.google.android.mms.pdu_alt.PduBody
1515import com.google.android.mms.pdu_alt.PduComposer
1616import com.google.android.mms.pdu_alt.PduPart
1717import com.google.android.mms.pdu_alt.SendReq
18+ import okhttp3.MediaType
19+ import java.io.File
1820
1921class MyFirebaseMessagingService : FirebaseMessagingService () {
2022 // [START receive_message]
@@ -177,20 +179,35 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
177179 return handleMultipartMessage(message, parts)
178180 }
179181
182+ fun extractFileName (url : String , prefix : String , mimeType : String? = null): String {
183+ val fileName = url.substringAfterLast(" /" )
184+ .substringBefore(" ?" )
185+ .takeIf { it.isNotBlank() && it.contains(" ." ) }
186+ ? : run {
187+ val extension = mimeType?.let { mime ->
188+ val ext = mime.substringAfterLast(" /" )
189+ if (ext.isNotBlank()) " .$ext " else " .bin"
190+ } ? : " "
191+ " attachment$extension "
192+ }
193+
194+ return " ${prefix} _$fileName "
195+ }
196+
180197 private fun handleMmsMessage (message : Message ): Result {
181198 Timber .d(" Processing MMS for message ID [${message.id} ]" )
182199 val apiService = HttpSmsApiService .create(applicationContext)
183200
184- val downloadedFiles = mutableListOf< java.io. File > ()
201+ val downloadedFiles = mutableListOf<Pair < File , MediaType > >()
185202
186203 try {
187204 for ((index, attachment) in message.attachments!! .withIndex()) {
188- val file = apiService.downloadAttachment(applicationContext, attachment.url , message.id, index)
189- if (file == null ) {
205+ val file = apiService.downloadAttachment(applicationContext, attachment, message.id, index)
206+ if (file.first == null || file.second == null ) {
190207 handleFailed(applicationContext, message.id, " Failed to download attachment or file size exceeded 1.5MB." )
191208 return Result .failure()
192209 }
193- downloadedFiles.add(file)
210+ downloadedFiles.add(Pair ( file.first !! , file.second !! ) )
194211 }
195212
196213 val sendReq = SendReq ()
@@ -207,30 +224,32 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
207224 textPart.name = " text" .toByteArray()
208225 textPart.contentId = " text" .toByteArray()
209226 textPart.contentLocation = " text" .toByteArray()
210-
227+
211228 var messageBody = message.content
212229 val encryptionKey = Settings .getEncryptionKey(applicationContext)
213230 if (message.encrypted && ! encryptionKey.isNullOrEmpty()) {
214231 messageBody = Encrypter .decrypt(encryptionKey, messageBody)
215232 }
216233 textPart.data = messageBody.toByteArray(Charsets .UTF_8 )
217-
234+
218235 pduBody.addPart(textPart)
219236 }
220237
221238 for ((index, file) in downloadedFiles.withIndex()) {
222- val attachment = message.attachments[index]
223- val fileBytes = file.readBytes()
239+ val fileBytes = file.first.readBytes()
224240
225241 val mediaPart = PduPart ()
226- mediaPart.contentType = attachment.contentType.toByteArray()
227-
228- val fileName = " attachment_$index " .toByteArray()
229- mediaPart.name = fileName
230- mediaPart.contentId = fileName
231- mediaPart.contentLocation = fileName
242+ mediaPart.contentType = file.second.toString().toByteArray()
243+
244+
245+ val fileName = extractFileName(message.attachments[index], index.toString(), file.second.toString())
246+ mediaPart.name = fileName.toByteArray()
247+ mediaPart.contentId = fileName.toByteArray()
248+ mediaPart.contentLocation = fileName.toByteArray()
232249 mediaPart.data = fileBytes
233-
250+
251+ Timber .d(" Adding MMS attachment with name [$fileName ] and size [${fileBytes.size} ] and type [${file.second} ]" )
252+
234253 pduBody.addPart(mediaPart)
235254 }
236255
@@ -249,7 +268,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
249268 if (! mmsDir.exists()) {
250269 mmsDir.mkdirs()
251270 }
252-
271+
253272 val pduFile = java.io.File (mmsDir, " pdu_${message.id} .dat" )
254273 java.io.FileOutputStream (pduFile).use { it.write(pduBytes) }
255274
@@ -272,15 +291,18 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
272291 } finally {
273292 // Clean up any downloaded temporary files
274293 downloadedFiles.forEach { file ->
275- if (file.exists()) {
276- file.delete()
294+ if (file.first. exists()) {
295+ file.first. delete()
277296 }
278297 }
279298
280299 // Also clean up the MMS PDU file to avoid cache buildup in cases where
281300 // sendMultimediaMessage fails before the sent broadcast is delivered.
282301 try {
283- val pduFile = java.io.File (applicationContext.cacheDir, " pdu_${message.id} .dat" )
302+ // The PDU file is stored under the "mms_attachments" cache subdirectory;
303+ // delete it from the same location to ensure cleanup is effective.
304+ val pduDir = File (applicationContext.cacheDir, " mms_attachments" )
305+ val pduFile = File (pduDir, " pdu_${message.id} .dat" )
284306 if (pduFile.exists()) {
285307 val deleted = pduFile.delete()
286308 if (! deleted) {
0 commit comments