@@ -32,6 +32,7 @@ import coil.request.ImageRequest
32
32
import coil.size.Size
33
33
import com.google.firebase.messaging.FirebaseMessagingService
34
34
import com.google.firebase.messaging.RemoteMessage
35
+ import com.troplo.privateuploader.api.SessionManager
35
36
import com.troplo.privateuploader.api.TpuApi
36
37
import com.troplo.privateuploader.api.TpuFunctions
37
38
import com.troplo.privateuploader.api.imageLoader
@@ -60,7 +61,7 @@ class FirebaseChatService : FirebaseMessagingService() {
60
61
61
62
override fun onMessageReceived (remoteMessage : RemoteMessage ) {
62
63
Log .d(TAG , " [NewChatService] Message received" )
63
- if (isAppOnForeground(this )) {
64
+ if (isAppOnForeground(this ) && remoteMessage.data[ " type " ] == " message " ) {
64
65
Log .d(TAG , " [NewChatService] App is on foreground" )
65
66
return
66
67
}
@@ -73,7 +74,8 @@ class FirebaseChatService : FirebaseMessagingService() {
73
74
chatName = remoteMessage.data[" chatName" ] ? : " " ,
74
75
associationId = remoteMessage.data[" associationId" ]?.toInt() ? : 0 ,
75
76
avatar = remoteMessage.data[" avatar" ] ? : " " ,
76
- id = remoteMessage.data[" id" ]?.toInt() ? : 0
77
+ id = remoteMessage.data[" id" ]?.toInt() ? : 0 ,
78
+ type = remoteMessage.data[" type" ] ? : " "
77
79
)
78
80
)
79
81
}
@@ -140,95 +142,121 @@ class FirebaseChatService : FirebaseMessagingService() {
140
142
// for ActivityCompat#requestPermissions for more details.
141
143
return
142
144
}
143
- asyncLoadIcon(message.avatar, this ) {
144
- try {
145
- Log .d(" TPU.Untagged" , " [ChatService] Loaded icon" )
146
- val chatPartner = Person .Builder ().apply {
147
- setName(message.username)
148
- setKey(message.userId.toString())
149
- setIcon(it)
150
- setImportant(false )
151
- }.build()
152
-
153
- val notificationManager = NotificationManagerCompat .from(this )
154
- val channel = NotificationChannel (
155
- " communications" ,
156
- " Messages from Communications" ,
157
- NotificationManager .IMPORTANCE_HIGH
158
- )
159
- notificationManager.createNotificationChannel(channel)
160
- if (messages[message.associationId] == null ) messages[message.associationId] =
161
- mutableListOf ()
162
- messages[message.associationId]?.add(
163
- NotificationCompat .MessagingStyle .Message (
164
- message.content,
165
- TpuFunctions .getDate(message.createdAt)?.time ? : 0 ,
166
- chatPartner
145
+
146
+ if (message.type == " message" ) {
147
+ asyncLoadIcon(message.avatar, this ) {
148
+ try {
149
+ Log .d(" TPU.Untagged" , " [ChatService] Loaded icon" )
150
+ val chatPartner = Person .Builder ().apply {
151
+ setName(message.username)
152
+ setKey(message.userId.toString())
153
+ setIcon(it)
154
+ setImportant(false )
155
+ }.build()
156
+
157
+ val notificationManager = NotificationManagerCompat .from(this )
158
+ val channel = NotificationChannel (
159
+ " communications" ,
160
+ " Messages from Communications" ,
161
+ NotificationManager .IMPORTANCE_HIGH
167
162
)
168
- )
163
+ notificationManager.createNotificationChannel(channel)
164
+ if (messages[message.associationId] == null ) messages[message.associationId] =
165
+ mutableListOf ()
169
166
170
- val style = NotificationCompat .MessagingStyle (chatPartner)
171
- .setConversationTitle(message.chatName)
172
167
173
- for (msg in messages[message.associationId]!! ) {
174
- style.addMessage(msg)
175
- }
168
+ Log .d(" TPU.Firebase" , " [ChatService] Secure message" )
169
+ val rep = Intent (this , InlineNotificationActivity ::class .java)
170
+ rep.replaceExtras(Bundle ())
171
+ rep.putExtra(" chatId" , message.associationId)
172
+ Log .d(" TPU.Firebase" , " [ChatService] ${rep.extras} " )
173
+ val style = NotificationCompat .MessagingStyle (chatPartner)
174
+ .setConversationTitle(message.chatName)
175
+ val replyPendingIntent = PendingIntent .getBroadcast(
176
+ this @FirebaseChatService,
177
+ message.associationId,
178
+ rep,
179
+ PendingIntent .FLAG_MUTABLE
180
+ )
181
+
182
+ val remoteInput = RemoteInput .Builder (" content" )
183
+ .setLabel(" Reply" )
184
+ .build()
185
+
186
+ val replyAction = NotificationCompat .Action .Builder (
187
+ R .drawable.flowinity_logo,
188
+ " Reply" ,
189
+ replyPendingIntent
190
+ )
191
+ .addRemoteInput(remoteInput)
192
+ .setAllowGeneratedReplies(true )
193
+ .build()
194
+ Log .d(" TPU.Firebase" , " [ChatService] ${message.associationId} " )
195
+ val builder: NotificationCompat .Builder =
196
+ NotificationCompat .Builder (this , " communications" )
197
+ .addPerson(chatPartner)
198
+ .setContentText(message.content)
199
+ .setContentTitle(message.username)
200
+ .setSmallIcon(R .drawable.flowinity_logo)
201
+ .setWhen(TpuFunctions .getDate(message.createdAt)?.time ? : 0 )
202
+ .addAction(replyAction)
203
+ .setContentIntent(
204
+ PendingIntent .getActivity(
205
+ this ,
206
+ message.associationId,
207
+ Intent (this , MainActivity ::class .java).apply {
208
+ putExtra(" chatId" , message.associationId)
209
+ },
210
+ PendingIntent .FLAG_MUTABLE
211
+ )
212
+ )
213
+ CoroutineScope (Dispatchers .IO ).launch {
214
+ TpuApi .init (SessionManager (this @FirebaseChatService).getAuthToken() ? : " " , this @FirebaseChatService)
215
+ val messageRequest = TpuApi .retrofitService.getMessage(
216
+ messageId = message.id
217
+ ).execute()
176
218
177
- val rep = Intent (this , InlineNotificationActivity ::class .java)
178
- rep.replaceExtras(Bundle ())
179
- rep.putExtra(" chatId" , message.associationId)
180
- val replyPendingIntent = PendingIntent .getBroadcast(
181
- this ,
182
- message.associationId,
183
- rep,
184
- PendingIntent .FLAG_MUTABLE
185
- )
186
-
187
- val remoteInput = RemoteInput .Builder (" content" )
188
- .setLabel(" Reply" )
189
- .build()
190
-
191
- val replyAction = NotificationCompat .Action .Builder (
192
- R .drawable.flowinity_logo,
193
- " Reply" ,
194
- replyPendingIntent
195
- )
196
- .addRemoteInput(remoteInput)
197
- .setAllowGeneratedReplies(true )
198
- .build()
199
-
200
- val builder: NotificationCompat .Builder =
201
- NotificationCompat .Builder (this , " communications" )
202
- .addPerson(chatPartner)
203
- .setStyle(style)
204
- .setContentText(message.content)
205
- .setContentTitle(message.username)
206
- .setSmallIcon(R .drawable.flowinity_logo)
207
- .setWhen(TpuFunctions .getDate(message.createdAt)?.time ? : 0 )
208
- .addAction(replyAction)
209
- .setContentIntent(
210
- PendingIntent .getActivity(
211
- this ,
212
- message.associationId,
213
- Intent (this , MainActivity ::class .java).apply {
214
- putExtra(" chatId" , message.associationId)
215
- },
216
- PendingIntent .FLAG_MUTABLE
219
+ if (messageRequest.isSuccessful) {
220
+ Log .d(
221
+ " TPU.Firebase" ,
222
+ " New message came through, ${messageRequest.body()?.content} "
217
223
)
218
- )
219
- val res = notificationManager.notify(message.associationId, builder.build())
220
- Log .d(" TPU.Untagged" , " [ChatService] Notification sent, $res " )
221
- } catch (e: Exception ) {
222
- Log .d(
223
- " TPU.Untagged" ,
224
- " [ChatService] Error sending notification, ${e.printStackTrace()} "
225
- )
224
+ messages[message.associationId]?.add(
225
+ NotificationCompat .MessagingStyle .Message (
226
+ messageRequest.body()?.content ? : " " ,
227
+ TpuFunctions .getDate(message.createdAt)?.time ? : 0 ,
228
+ chatPartner
229
+ )
230
+ )
231
+ }
232
+
233
+ Log .d(" TPU.Firebase" , " [ChatService] Added message to list" )
234
+
235
+ for (msg in messages[message.associationId]!! ) {
236
+ style.addMessage(msg)
237
+ }
238
+
239
+ builder.setStyle(style)
240
+
241
+ val res = notificationManager.notify(message.associationId, builder.build())
242
+ Log .d(" TPU.Untagged" , " [ChatService] Notification sent, $res " )
243
+ }
244
+ } catch (e: Exception ) {
245
+ Log .d(
246
+ " TPU.Untagged" ,
247
+ " [ChatService] Error sending notification, ${e.printStackTrace()} "
248
+ )
249
+ }
226
250
}
251
+ } else if (message.type == " read" ) {
252
+ val notificationManager = NotificationManagerCompat .from(this )
253
+ notificationManager.cancel(message.associationId)
227
254
}
228
255
}
229
256
230
257
companion object {
231
258
private const val TAG = " FirebaseChatService"
259
+ private const val FAKE_MESSAGE_CONTENT = " Please update your version of the Flowinity app."
232
260
}
233
261
234
262
internal class MyWorker (appContext : Context , workerParams : WorkerParameters ) :
0 commit comments