11package xyz.flipchat.app.services
22
3+ import android.Manifest
34import android.app.Notification
45import android.app.NotificationChannel
56import android.app.NotificationManager
67import android.app.PendingIntent
78import android.content.Context
89import android.content.Intent
10+ import android.content.pm.PackageManager
911import android.media.RingtoneManager
1012import android.os.Build
13+ import androidx.core.app.ActivityCompat
1114import androidx.core.app.NotificationCompat
15+ import androidx.core.app.NotificationManagerCompat
1216import androidx.core.app.Person
1317import com.getcode.model.ID
1418import 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-
223223private fun Context.buildContentIntent (type : FcNotificationType ): PendingIntent {
224224 val launchIntent = when (type) {
225225 is FcNotificationType .ChatMessage -> Intent (" https://app.flipchat.xyz/chat/${type.id?.base58} " )
0 commit comments