@@ -14,6 +14,8 @@ import coil3.ImageLoader
1414import coil3.request.ImageRequest
1515import com.google.firebase.messaging.FirebaseMessagingService
1616import com.google.firebase.messaging.RemoteMessage
17+ import com.terning.core.analytics.AmplitudeTracker
18+ import com.terning.core.analytics.EventType
1719import com.terning.core.designsystem.type.NotificationRedirect
1820import com.terning.core.designsystem.util.DeeplinkDefaults
1921import com.terning.core.designsystem.util.DeeplinkDefaults.REDIRECT
@@ -34,6 +36,9 @@ class TerningMessagingService : FirebaseMessagingService() {
3436 @Inject
3537 lateinit var navigatorProvider: NavigatorProvider
3638
39+ @Inject
40+ lateinit var amplitudeTracker: AmplitudeTracker
41+
3742 override fun onNewToken (token : String ) {
3843 super .onNewToken(token)
3944
@@ -43,23 +48,27 @@ class TerningMessagingService : FirebaseMessagingService() {
4348 override fun handleIntent (intent : Intent ? ) {
4449 super .handleIntent(intent)
4550
46- extractInformation(
47- title = intent?.getStringExtra(TITLE ),
48- body = intent?.getStringExtra(BODY ),
49- type = intent?.getStringExtra(TYPE ),
50- imageUrl = intent?.getStringExtra(IMAGE_URL )
51- )
51+ if (! isAppInForeground()) {
52+ extractInformation(
53+ title = intent?.getStringExtra(TITLE ),
54+ body = intent?.getStringExtra(BODY ),
55+ type = intent?.getStringExtra(TYPE ),
56+ imageUrl = intent?.getStringExtra(IMAGE_URL )
57+ )
58+ }
5259 }
5360
5461 override fun onMessageReceived (message : RemoteMessage ) {
5562 super .onMessageReceived(message)
5663
57- extractInformation(
58- title = message.data[TITLE ],
59- body = message.data[BODY ],
60- type = message.data[TYPE ],
61- imageUrl = message.data[IMAGE_URL ]
62- )
64+ if (isAppInForeground()) {
65+ extractInformation(
66+ title = message.data[TITLE ],
67+ body = message.data[BODY ],
68+ type = message.data[TYPE ],
69+ imageUrl = message.data[IMAGE_URL ]
70+ )
71+ }
6372 }
6473
6574 private fun extractInformation (
@@ -70,6 +79,11 @@ class TerningMessagingService : FirebaseMessagingService() {
7079 ) {
7180 if (title.isNullOrEmpty() || ! userRepository.getAlarmAvailable()) return
7281
82+ amplitudeTracker.track(
83+ type = EventType .PUSH_NOTIFICATION ,
84+ name = " received"
85+ )
86+
7387 sendNotification(
7488 title = title,
7589 body = body.orEmpty(),
@@ -91,6 +105,7 @@ class TerningMessagingService : FirebaseMessagingService() {
91105 action = Intent .ACTION_VIEW
92106 data = deeplink.toUri()
93107 flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TOP
108+ putExtra(FROM_NOTIFICATION , true )
94109 }
95110 val pendingIntent = PendingIntent .getActivity(
96111 this @TerningMessagingService,
@@ -159,5 +174,6 @@ class TerningMessagingService : FirebaseMessagingService() {
159174 private const val BODY : String = " body"
160175 private const val TYPE : String = " type"
161176 private const val IMAGE_URL : String = " imageUrl"
177+ const val FROM_NOTIFICATION : String = " fromNotification"
162178 }
163179}
0 commit comments