Skip to content

Commit ad42859

Browse files
committed
1.0.14 - New FCM handling
1 parent 14b1b1b commit ad42859

File tree

11 files changed

+157
-106
lines changed

11 files changed

+157
-106
lines changed

.idea/deploymentTargetDropDown.xml

+1-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ android {
3131
applicationId = "com.troplo.privateuploader"
3232
minSdk = 28
3333
targetSdk = 34
34-
versionCode = 13
35-
versionName = "1.0.13"
34+
versionCode = 14
35+
versionName = "1.0.14"
3636
multiDexEnabled = true
3737
buildConfigField("String", "SERVER_URL", "\"https://flowinity.com\"")
3838
buildConfigField("String", "BUILD_TIME", "\"${DateFormat.getDateTimeInstance().format(System.currentTimeMillis())}\"")

app/src/main/java/com/troplo/privateuploader/FirebaseChatService.kt

+108-80
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import coil.request.ImageRequest
3232
import coil.size.Size
3333
import com.google.firebase.messaging.FirebaseMessagingService
3434
import com.google.firebase.messaging.RemoteMessage
35+
import com.troplo.privateuploader.api.SessionManager
3536
import com.troplo.privateuploader.api.TpuApi
3637
import com.troplo.privateuploader.api.TpuFunctions
3738
import com.troplo.privateuploader.api.imageLoader
@@ -60,7 +61,7 @@ class FirebaseChatService : FirebaseMessagingService() {
6061

6162
override fun onMessageReceived(remoteMessage: RemoteMessage) {
6263
Log.d(TAG, "[NewChatService] Message received")
63-
if (isAppOnForeground(this)) {
64+
if (isAppOnForeground(this) && remoteMessage.data["type"] == "message") {
6465
Log.d(TAG, "[NewChatService] App is on foreground")
6566
return
6667
}
@@ -73,7 +74,8 @@ class FirebaseChatService : FirebaseMessagingService() {
7374
chatName = remoteMessage.data["chatName"] ?: "",
7475
associationId = remoteMessage.data["associationId"]?.toInt() ?: 0,
7576
avatar = remoteMessage.data["avatar"] ?: "",
76-
id = remoteMessage.data["id"]?.toInt() ?: 0
77+
id = remoteMessage.data["id"]?.toInt() ?: 0,
78+
type = remoteMessage.data["type"] ?: ""
7779
)
7880
)
7981
}
@@ -140,95 +142,121 @@ class FirebaseChatService : FirebaseMessagingService() {
140142
// for ActivityCompat#requestPermissions for more details.
141143
return
142144
}
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
167162
)
168-
)
163+
notificationManager.createNotificationChannel(channel)
164+
if (messages[message.associationId] == null) messages[message.associationId] =
165+
mutableListOf()
169166

170-
val style = NotificationCompat.MessagingStyle(chatPartner)
171-
.setConversationTitle(message.chatName)
172167

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()
176218

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}"
217223
)
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+
}
226250
}
251+
} else if(message.type == "read") {
252+
val notificationManager = NotificationManagerCompat.from(this)
253+
notificationManager.cancel(message.associationId)
227254
}
228255
}
229256

230257
companion object {
231258
private const val TAG = "FirebaseChatService"
259+
private const val FAKE_MESSAGE_CONTENT = "Please update your version of the Flowinity app."
232260
}
233261

234262
internal class MyWorker(appContext: Context, workerParams: WorkerParameters) :

app/src/main/java/com/troplo/privateuploader/InlineNotificationActivity.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ class InlineNotificationActivity : BroadcastReceiver() {
1818
try {
1919
Log.d(
2020
"TPU.Untagged",
21-
"[ChatService] InlineNotificationActivity onCreate, intent: $intent, extras: ${intent.extras}"
21+
"[Firebase] InlineNotificationActivity onCreate, intent: $intent, extras: ${intent.extras}"
2222
)
2323

2424
val chatId = intent.getIntExtra("chatId", 0)
2525
val remoteInput = RemoteInput.getResultsFromIntent(intent)
2626
val content = remoteInput?.getCharSequence("content")?.toString()
27+
Log.d("InlineNotificationAct", "Firebase - chatId: $chatId, content: $content")
2728
TpuApi.init(SessionManager(context).getAuthToken() ?: "", context)
2829
sendReply(chatId, content, context)
2930
} catch (e: Exception) {
30-
Log.d("TPU.InlineNotificationActivity", "Exception: $e")
31+
Log.d("TPU.InlineNotificationAct", "Firebase - Exception: $e")
3132
}
3233
}
3334

3435
private fun sendReply(chatId: Int, content: String?, context: Context) {
3536
try {
3637
if (chatId == 0) return
37-
Log.d("TPU.Untagged", "Sending reply to chatId: $chatId")
38+
Log.d("TPU.Untagged", "Firebase - Sending reply to chatId: $chatId")
3839
CoroutineScope(Dispatchers.IO).launch {
3940
val response = TpuApi.retrofitService.sendMessage(
4041
id = chatId, messageRequest = MessageRequest(
@@ -43,7 +44,7 @@ class InlineNotificationActivity : BroadcastReceiver() {
4344
).execute()
4445
}
4546
} catch (e: Exception) {
46-
Log.d("TPU.InlineNotificationActivity", "sendReply exception: $e")
47+
Log.d("TPU.InlineNotificationAct", "Firebase - sendReply exception: $e")
4748
}
4849
}
4950
}

app/src/main/java/com/troplo/privateuploader/MainActivity.kt

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.troplo.privateuploader.api.SocketHandler
2323
import com.troplo.privateuploader.api.SocketHandlerService
2424
import com.troplo.privateuploader.api.TpuApi
2525
import com.troplo.privateuploader.api.TpuFunctions
26+
import com.troplo.privateuploader.api.stores.AppStore
2627
import com.troplo.privateuploader.api.stores.CollectionStore
2728
import com.troplo.privateuploader.api.stores.CoreStore
2829
import com.troplo.privateuploader.api.stores.UploadStore
@@ -42,17 +43,25 @@ import okhttp3.RequestBody.Companion.asRequestBody
4243

4344

4445
class MainActivity : ComponentActivity() {
46+
4547
override fun onResume() {
4648
super.onResume()
49+
AppStore.foreground = true
4750
GoogleApiAvailability.getInstance().makeGooglePlayServicesAvailable(this)
4851
val socket = SocketHandler.getSocket()
4952
if (socket != null && !socket.connected()) {
5053
socket.connect()
5154
}
5255
}
5356

57+
override fun onPause() {
58+
super.onPause()
59+
AppStore.foreground = false
60+
}
61+
5462
override fun onStart() {
5563
super.onStart()
64+
AppStore.foreground = true
5665
val socket = SocketHandler.getSocket()
5766
if (socket != null && !socket.connected()) {
5867
socket.connect()

app/src/main/java/com/troplo/privateuploader/api/ApiService.kt

+5
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@ object TpuApi {
389389
@Path("associationId") associationId: Int,
390390
@Body pinRequest: PinRequest
391391
): Call<Unit>
392+
393+
@GET("chats/messages/{messageId}")
394+
fun getMessage(
395+
@Path("messageId") messageId: Int
396+
): Call<Message>
392397
}
393398

394399
val retrofitService: TpuApiService by lazy {

app/src/main/java/com/troplo/privateuploader/api/stores/AppStore.kt

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ import androidx.navigation.NavController
55

66
object AppStore {
77
var navController: NavController? = null
8+
var foreground = false
89
}

app/src/main/java/com/troplo/privateuploader/api/stores/ChatStore.kt

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.Context
44
import android.util.Log
55
import androidx.compose.runtime.mutableStateListOf
66
import androidx.compose.runtime.mutableStateOf
7+
import com.troplo.privateuploader.api.stores.AppStore
78
import com.troplo.privateuploader.data.model.AddChatUsersEvent
89
import com.troplo.privateuploader.data.model.Chat
910
import com.troplo.privateuploader.data.model.PinRequest
@@ -100,11 +101,16 @@ object ChatStore {
100101
associationId.value = id
101102

102103
// Handle unread count, and init read receipt
104+
// Ensure that the app is in the foreground
105+
103106
val socket = SocketHandler.getSocket()
104-
socket?.emit("readChat", id)
105-
val chat = chats.find { it.association?.id == id }
106-
if (chat != null) {
107-
chat.unread = 0
107+
Log.d("MarkAsRead", "Foreground: ${AppStore.foreground}")
108+
if(AppStore.foreground) {
109+
socket?.emit("readChat", id)
110+
val chat = chats.find { it.association?.id == id }
111+
if (chat != null) {
112+
chat.unread = 0
113+
}
108114
}
109115
}
110116

app/src/main/java/com/troplo/privateuploader/components/core/NavRoute.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ fun getCurrentRouteTitle(route: String): String {
1212
NavRoute.SettingsPreferences.path -> "Preferences"
1313
NavRoute.Friends.path -> "Friends"
1414
NavRoute.SettingsCollections.path -> "Collections"
15-
else -> "PrivateUploader"
15+
NavRoute.Notifications.path -> "Notifications"
16+
else -> "Flowinity"
1617
}
1718
}
1819

app/src/main/java/com/troplo/privateuploader/data/model/Message.kt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ data class MessageEventFirebase(
6666
@field:Json(name = "avatar") val avatar: String,
6767
@field:Json(name = "chatName") val chatName: String,
6868
@field:Json(name = "createdAt") val createdAt: String,
69+
@field:Json(name = "type") val type: String,
6970
)
7071

7172
@JsonClass(generateAdapter = true)

0 commit comments

Comments
 (0)