Skip to content

Commit d47bfdd

Browse files
committed
chore(fc): properly use injected NotificationManager
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 4b1b206 commit d47bfdd

File tree

4 files changed

+38
-38
lines changed

4 files changed

+38
-38
lines changed

flipchatApp/src/main/kotlin/xyz/flipchat/app/inject/AppModule.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package xyz.flipchat.app.inject
22

33
import android.annotation.SuppressLint
4-
import android.app.NotificationManager
54
import android.content.ClipboardManager
65
import android.content.Context
76
import android.net.ConnectivityManager

flipchatApp/src/main/kotlin/xyz/flipchat/app/services/FcNotificationService.kt

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package xyz.flipchat.app.services
22

3+
import android.Manifest
34
import android.app.Notification
45
import android.app.NotificationChannel
56
import android.app.NotificationManager
67
import android.app.PendingIntent
78
import android.content.Context
89
import android.content.Intent
10+
import android.content.pm.PackageManager
911
import android.media.RingtoneManager
1012
import android.os.Build
13+
import androidx.core.app.ActivityCompat
1114
import androidx.core.app.NotificationCompat
15+
import androidx.core.app.NotificationManagerCompat
1216
import androidx.core.app.Person
1317
import com.getcode.model.ID
1418
import com.getcode.ui.components.chat.utils.localizedText
@@ -62,6 +66,9 @@ class FcNotificationService : FirebaseMessagingService(),
6266
@Inject
6367
lateinit var currencyUtils: CurrencyUtils
6468

69+
@Inject
70+
lateinit var notificationManager: NotificationManagerCompat
71+
6572
override fun onMessageReceived(message: RemoteMessage) {
6673
super.onMessageReceived(message)
6774
authenticateIfNeeded { handleMessage(message) }
@@ -138,9 +145,6 @@ class FcNotificationService : FirebaseMessagingService(),
138145
title: String,
139146
content: String,
140147
) {
141-
val notificationManager =
142-
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
143-
144148
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
145149
notificationManager.createNotificationChannel(
146150
NotificationChannel(
@@ -179,21 +183,35 @@ class FcNotificationService : FirebaseMessagingService(),
179183
.setAutoCancel(true)
180184
.setContentIntent(buildContentIntent(type))
181185

182-
notificationManager.notify(title.hashCode(), notificationBuilder.build())
183-
184-
trace(
185-
tag = "Push",
186-
message = "Push notification shown",
187-
metadata = {
188-
"category" to type.name
189-
},
190-
type = TraceType.Process
191-
)
186+
if (ActivityCompat.checkSelfPermission(
187+
this,
188+
Manifest.permission.POST_NOTIFICATIONS
189+
) == PackageManager.PERMISSION_GRANTED
190+
) {
191+
notificationManager.notify(title.hashCode(), notificationBuilder.build())
192+
trace(
193+
tag = "Push",
194+
message = "Push notification shown",
195+
metadata = {
196+
"category" to type.name
197+
},
198+
type = TraceType.Process
199+
)
200+
} else {
201+
trace(
202+
tag = "Push",
203+
message = "Push notification NOT shown - missing permission",
204+
metadata = {
205+
"category" to type.name
206+
},
207+
type = TraceType.Process
208+
)
209+
}
192210
}
193211
}
194212

195-
private fun NotificationManager.getActiveNotification(notificationId: Int): Notification? {
196-
val barNotifications = getActiveNotifications()
213+
private fun NotificationManagerCompat.getActiveNotification(notificationId: Int): Notification? {
214+
val barNotifications = activeNotifications
197215
for (notification in barNotifications) {
198216
if (notification.id == notificationId) {
199217
return notification.notification
@@ -202,24 +220,6 @@ private fun NotificationManager.getActiveNotification(notificationId: Int): Noti
202220
return null
203221
}
204222

205-
fun NotificationManager.getRoomNotifications(roomId: ID, roomName: String): List<Notification> {
206-
val barNotifications = getActiveNotifications()
207-
val roomNotifications = barNotifications.mapNotNull { notification ->
208-
val roomIdHash = roomId.base58.hashCode()
209-
val roomNameHash = roomName.hashCode()
210-
211-
val isMatch = notification.id == roomIdHash || notification.id == roomNameHash
212-
213-
if (isMatch) {
214-
notification.notification
215-
} else {
216-
null
217-
}
218-
}
219-
220-
return roomNotifications
221-
}
222-
223223
private fun Context.buildContentIntent(type: FcNotificationType): PendingIntent {
224224
val launchIntent = when (type) {
225225
is FcNotificationType.ChatMessage -> Intent("https://app.flipchat.xyz/chat/${type.id?.base58}")

services/flipchat/sdk/src/main/kotlin/xyz/flipchat/chat/RoomController.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package xyz.flipchat.chat
22

33
import android.app.NotificationManager
4+
import androidx.core.app.NotificationManagerCompat
45
import androidx.paging.ExperimentalPagingApi
56
import androidx.paging.LoadType
67
import androidx.paging.Pager
@@ -36,7 +37,7 @@ class RoomController @Inject constructor(
3637
private val messagingRepository: MessagingRepository,
3738
private val conversationMemberMapper: ConversationMemberMapper,
3839
private val conversationMessageWithContentMapper: ConversationMessageWithContentMapper,
39-
private val notificationManager: NotificationManager,
40+
private val notificationManager: NotificationManagerCompat,
4041
) {
4142
private val db: FcAppDatabase
4243
get() = FcAppDatabase.requireInstance()

services/flipchat/sdk/src/main/kotlin/xyz/flipchat/notifications/NotificationManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package xyz.flipchat.notifications
22

3-
import android.app.NotificationManager
3+
import androidx.core.app.NotificationManagerCompat
44
import com.getcode.model.ID
55
import com.getcode.utils.base58
66

7-
fun NotificationManager.getRoomNotifications(roomId: ID, roomName: String): List<Int> {
8-
val barNotifications = getActiveNotifications()
7+
fun NotificationManagerCompat.getRoomNotifications(roomId: ID, roomName: String): List<Int> {
8+
val barNotifications = activeNotifications
99
val roomNotifications = barNotifications.mapNotNull { notification ->
1010
val roomIdHash = roomId.base58.hashCode()
1111
val roomNameHash = roomName.hashCode()

0 commit comments

Comments
 (0)