1- package xyz.flipchat.app.services
1+ package xyz.flipchat.app.notifications
22
33import android.Manifest
44import android.app.Notification
@@ -17,7 +17,6 @@ import androidx.core.app.NotificationCompat
1717import androidx.core.app.NotificationManagerCompat
1818import androidx.core.app.Person
1919import androidx.core.app.RemoteInput
20- import com.getcode.model.ID
2120import com.getcode.ui.components.chat.utils.localizedText
2221import com.getcode.util.resources.ResourceHelper
2322import com.getcode.util.resources.ResourceType
@@ -38,14 +37,13 @@ import xyz.flipchat.app.R
3837import xyz.flipchat.app.auth.AuthManager
3938import xyz.flipchat.app.theme.FC_Primary
4039import xyz.flipchat.controllers.ChatsController
41- import xyz.flipchat.controllers.CodeController
4240import xyz.flipchat.controllers.PushController
4341import xyz.flipchat.notifications.FcNotificationType
4442import xyz.flipchat.notifications.parse
43+ import xyz.flipchat.services.user.AuthState
4544import xyz.flipchat.services.user.UserManager
4645import java.security.SecureRandom
4746import javax.inject.Inject
48- import kotlin.random.Random
4947
5048@AndroidEntryPoint
5149class FcNotificationService : FirebaseMessagingService (),
@@ -164,108 +162,22 @@ class FcNotificationService : FirebaseMessagingService(),
164162 return null
165163 }
166164
167- val (id, notification) = when (type) {
168- is FcNotificationType .ChatMessage -> buildChatNotification(type, title, content)
169- FcNotificationType .Unknown -> buildMiscNotification(type, title, content)
170- }
171-
172- return id to notification.build()
173- }
174-
175- private fun buildChatNotification (
176- type : FcNotificationType .ChatMessage ,
177- title : String ,
178- content : String
179- ): Pair <Int , NotificationCompat .Builder > {
180- val sender = content.substringBefore(" :" )
181- val messageBody = content.substringAfter(" :" )
182- val person = Person .Builder ()
183- .setName(sender)
184- .build()
185-
186- val message = NotificationCompat .MessagingStyle .Message (
187- messageBody,
188- Clock .System .now().toEpochMilliseconds(),
189- person
190- )
191-
192- val notificationId = type.id?.base58.hashCode()
193-
194- val style = notificationManager.getActiveNotification(notificationId)?.let {
195- NotificationCompat .MessagingStyle .extractMessagingStyleFromNotification(it)
196- } ? : NotificationCompat .MessagingStyle (person)
197- .setConversationTitle(title)
198- .setGroupConversation(true )
199-
200- val updatedStyle = style.addMessage(message)
201-
202- val replyAction = if (type.id != null ) {
203- // build direct reply action
204- val replyLabel: String = resources.getString(R .string.action_reply)
205- val remoteInput: RemoteInput = RemoteInput .Builder (KEY_TEXT_REPLY ).run {
206- setLabel(replyLabel)
207- build()
208- }
209-
210- val resultIntent = Intent (applicationContext, FcNotificationReceiver ::class .java).apply {
211- putExtra(KEY_ROOM_ID , type.id!! .base58)
212- putExtra(KEY_NOTIFICATION_ID , notificationId)
213- }
214-
215- val replyPendingIntent: PendingIntent =
216- PendingIntent .getBroadcast(
165+ with (notificationManager) {
166+ val (id, notification) = when (type) {
167+ is FcNotificationType .ChatMessage -> buildChatNotification(
217168 applicationContext,
218- type.id.hashCode(),
219- resultIntent,
220- PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_MUTABLE
169+ resources,
170+ type,
171+ title,
172+ content,
173+ userManager.authState is AuthState .LoggedIn
221174 )
222175
223- NotificationCompat .Action .Builder (
224- R .drawable.ic_reply,
225- getString(R .string.action_reply),
226- replyPendingIntent
227- ).addRemoteInput(remoteInput).build()
228- } else {
229- null
230- }
231-
232- val notificationBuilder: NotificationCompat .Builder =
233- NotificationCompat .Builder (this , type.name)
234- .setPriority(NotificationCompat .PRIORITY_DEFAULT )
235- .setStyle(updatedStyle)
236- .setSound(RingtoneManager .getDefaultUri(RingtoneManager .TYPE_NOTIFICATION ))
237- .setSmallIcon(R .drawable.ic_flipchat_notification)
238- .setColor(FC_Primary .toArgb())
239- .setAutoCancel(true )
240- .setContentIntent(buildContentIntent(type))
176+ FcNotificationType .Unknown -> buildMiscNotification(applicationContext, type, title, content)
177+ }
241178
242- if (replyAction != null ) {
243- notificationBuilder.addAction(replyAction)
179+ return id to notification.build()
244180 }
245-
246- return notificationId to notificationBuilder
247- }
248-
249- private fun buildMiscNotification (
250- type : FcNotificationType ,
251- title : String ,
252- content : String
253- ): Pair <Int , NotificationCompat .Builder > {
254- val notificationBuilder: NotificationCompat .Builder =
255- NotificationCompat .Builder (this , type.name)
256- .setPriority(NotificationCompat .PRIORITY_DEFAULT )
257- .setSound(RingtoneManager .getDefaultUri(RingtoneManager .TYPE_NOTIFICATION ))
258- .setSmallIcon(R .drawable.ic_flipchat_notification)
259- .setColor(FC_Primary .toArgb())
260- .setAutoCancel(true )
261- .setContentTitle(title)
262- .setContentText(content)
263- .setContentIntent(buildContentIntent(type))
264-
265- val random = SecureRandom ()
266- val notificationId = random.nextInt(256 )
267-
268- return notificationId to notificationBuilder
269181 }
270182
271183 private fun notify (
@@ -300,35 +212,6 @@ class FcNotificationService : FirebaseMessagingService(),
300212 }
301213}
302214
303- private fun NotificationManagerCompat.getActiveNotification (notificationId : Int ): Notification ? {
304- val barNotifications = activeNotifications
305- for (notification in barNotifications) {
306- if (notification.id == notificationId) {
307- return notification.notification
308- }
309- }
310- return null
311- }
312-
313- private fun Context.buildContentIntent (type : FcNotificationType ): PendingIntent {
314- val launchIntent = when (type) {
315- is FcNotificationType .ChatMessage -> Intent (Intent .ACTION_VIEW ).apply {
316- data = Uri .parse(" https://app.flipchat.xyz/room?r=${type.id?.base58} " )
317- }
318-
319- FcNotificationType .Unknown -> Intent (this , MainActivity ::class .java).apply {
320- flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TASK
321- }
322- }
323-
324- return PendingIntent .getActivity(
325- this ,
326- type.ordinal,
327- launchIntent,
328- PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE
329- )
330- }
331-
332215private fun String.localizedStringByKey (resources : ResourceHelper ): String? {
333216 val name = this .replace(" ." , " _" )
334217 val resId = resources.getIdentifier(
0 commit comments