@@ -104,15 +104,48 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
104104 }
105105
106106 val message = getMessage(applicationContext, messageID) ? : return Result .failure()
107+ val parts = getMessageParts(applicationContext, message)
108+ if (parts.size == 1 ) {
109+ return handleSingleMessage(applicationContext, message)
110+ }
111+ return handleMultipartMessage(applicationContext, message, parts)
112+ }
107113
108- registerReceivers(applicationContext, message.id)
114+ private fun handleMultipartMessage (context : Context , message : Message , parts : ArrayList <String >): Result {
115+ registerReceivers(context, message.id)
116+
117+ Timber .d(" sending SMS for message with ID [${message.id} ]" )
118+ return try {
119+ // The trick here is to listen to the intent only on the last part
120+ val sentIntents = ArrayList <PendingIntent >()
121+ val deliveredIntents = ArrayList <PendingIntent >()
122+
123+ for (i in 0 until parts.size) {
124+ var id = " ${message.id} .$i "
125+ if (i == parts.size - 1 ) {
126+ id = message.id
127+ }
128+ sentIntents.add(createPendingIntent(id, SmsManagerService .sentAction(id)))
129+ deliveredIntents.add(createPendingIntent(id, SmsManagerService .deliveredAction(id)))
130+ }
131+ SmsManagerService ().sendMultipartMessage(this .applicationContext,message.contact, parts, sentIntents, deliveredIntents)
132+ Timber .d(" sent SMS for message with ID [${message.id} ] in [${parts.size} ] parts" )
133+ Result .success()
134+ } catch (e: Exception ) {
135+ Timber .e(e)
136+ Timber .d(" could not send SMS for message with ID [${message.id} ] in [${parts.size} ] parts" )
137+ Result .failure()
138+ }
139+ }
109140
141+
142+ private fun handleSingleMessage (context : Context , message : Message ): Result {
143+ registerReceivers(context, message.id)
110144 sendMessage(
111145 message,
112- createPendingIntent(message, SmsManagerService .sentAction(message.id)),
113- createPendingIntent(message, SmsManagerService .deliveredAction(message.id))
146+ createPendingIntent(message.id , SmsManagerService .sentAction(message.id)),
147+ createPendingIntent(message.id , SmsManagerService .deliveredAction(message.id))
114148 )
115-
116149 return Result .success()
117150 }
118151
@@ -149,7 +182,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
149182 private fun sendMessage (message : Message , sentIntent : PendingIntent , deliveredIntent : PendingIntent ) {
150183 Timber .d(" sending SMS for message with ID [${message.id} ]" )
151184 try {
152- SmsManagerService ().sendMessage (this .applicationContext, message, sentIntent, deliveredIntent)
185+ SmsManagerService ().sendTextMessage (this .applicationContext,message.contact, message.content , sentIntent, deliveredIntent)
153186 } catch (e: Exception ) {
154187 Timber .e(e)
155188 Timber .d(" could not send SMS for message with ID [${message.id} ]" )
@@ -158,9 +191,24 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
158191 Timber .d(" sent SMS for message with ID [${message.id} ]" )
159192 }
160193
161- private fun createPendingIntent (message : Message , action : String ): PendingIntent {
194+ private fun getMessageParts (context : Context , message : Message ): ArrayList <String > {
195+ Timber .d(" getting parts for message with ID [${message.id} ]" )
196+ return try {
197+ val parts = SmsManagerService ().messageParts(context, message.content)
198+ Timber .d(" message with ID [${message.id} ] has [${parts.size} ] parts" )
199+ parts
200+ } catch (e: Exception ) {
201+ Timber .e(e)
202+ Timber .d(" could not get parts message with ID [${message.id} ] returning [1] part with entire content" )
203+ val list = ArrayList <String >()
204+ list.add(message.content)
205+ list
206+ }
207+ }
208+
209+ private fun createPendingIntent (id : String , action : String ): PendingIntent {
162210 val intent = Intent (action)
163- intent.putExtra(Constants .KEY_MESSAGE_ID , message. id)
211+ intent.putExtra(Constants .KEY_MESSAGE_ID , id)
164212
165213 return PendingIntent .getBroadcast(
166214 this .applicationContext,
0 commit comments