@@ -9,16 +9,19 @@ import android.os.Build
99import android.util.Log
1010import androidx.core.app.NotificationCompat
1111import androidx.core.app.NotificationManagerCompat
12+ import androidx.core.app.TaskStackBuilder
13+ import androidx.core.content.res.ResourcesCompat
14+ import androidx.core.net.toUri
1215import coil.imageLoader
1316import coil.request.ImageRequest
1417import com.edricchan.studybuddy.R
1518import com.edricchan.studybuddy.constants.Constants
19+ import com.edricchan.studybuddy.core.compat.navigation.UriSettings
1620import com.edricchan.studybuddy.core.resources.notification.AppNotificationChannel
1721import com.edricchan.studybuddy.exts.android.buildIntent
1822import com.edricchan.studybuddy.exts.common.TAG
1923import com.edricchan.studybuddy.interfaces.NotificationAction
2024import com.edricchan.studybuddy.ui.modules.main.MainActivity
21- import com.edricchan.studybuddy.ui.modules.settings.SettingsActivity
2225import com.edricchan.studybuddy.ui.theming.dynamicColorPrimary
2326import com.edricchan.studybuddy.utils.NotificationUtils
2427import com.google.firebase.auth.ktx.auth
@@ -111,70 +114,64 @@ class StudyBuddyMessagingService : FirebaseMessagingService() {
111114 }
112115 }
113116
114- if (remoteMessage.data[" notificationActions" ] != null ) {
115- Log .d(TAG , " notificationActions: ${remoteMessage.data[" notificationActions" ]} " )
116- var notificationActions: List <NotificationAction > = listOf ()
117-
118- try {
119- notificationActions = remoteMessage.data[" notificationActions" ]?.let {
120- Json .decodeFromString(it)
121- } ? : listOf ()
117+ remoteMessage.data[" notificationActions" ]?.let { actions ->
118+ Log .d(TAG , " notificationActions: $actions " )
119+ val notificationActions: List <NotificationAction > = try {
120+ Json .decodeFromString(actions)
122121 } catch (e: Exception ) {
123122 Log .e(TAG , " Could not parse notification actions:" , e)
124123 Firebase .crashlytics.recordException(e)
124+ listOf ()
125125 }
126126
127127 for (notificationAction in notificationActions) {
128- // This property is set to 0 by default to indicate that no such icon exists
129- var icon = 0
130- val intent: Intent
131- var notificationPendingIntent: PendingIntent ? = null
132- val drawableIcon = resources.getIdentifier(
133- notificationAction.icon,
134- " drawable" ,
135- packageName
136- )
137- // getIdentifier returns Resources.ID_NULL (0) if no such resource exists
138- if (drawableIcon != Resources .ID_NULL ) {
139- icon = drawableIcon
140- }
128+ val icon = notificationAction.icon?.let {
129+ resources.getIdentifier(
130+ it,
131+ " drawable" ,
132+ packageName
133+ )
134+ } ? : ResourcesCompat .ID_NULL
141135
142- when (notificationAction.type) {
136+ val pIntent = when (notificationAction.type) {
143137 // TODO: Don't hardcode action types
144138 Constants .actionNotificationsSettingsIntent -> {
145- intent =
146- buildIntent<SettingsActivity >(this ) {
147- flags = Intent .FLAG_ACTIVITY_CLEAR_TOP
148- }
149- notificationPendingIntent = PendingIntent .getActivity(
150- this ,
151- 0 ,
152- intent,
153- PendingIntent . FLAG_ONE_SHOT
154- )
139+ TaskStackBuilder .create( this ). run {
140+ addNextIntentWithParentStack( buildIntent<MainActivity >(this @StudyBuddyMessagingService ) {
141+ action = Intent .ACTION_VIEW
142+ data = UriSettings .toUri()
143+ })
144+ getPendingIntent(
145+ 0 ,
146+ PendingIntent . FLAG_UPDATE_CURRENT or PendingIntent . FLAG_IMMUTABLE
147+ )
148+ }
155149 }
156150
157- else -> Log .w(
158- TAG ,
159- " Unknown action type ${notificationAction.type} specified for $notificationAction ."
160- )
151+ else -> {
152+ Log .w(
153+ TAG ,
154+ " Unknown action type ${notificationAction.type} specified for $notificationAction ."
155+ )
156+ null
157+ }
161158 }
162159 builder.addAction(
163160 NotificationCompat .Action (
164161 icon,
165162 notificationAction.title,
166- notificationPendingIntent
163+ pIntent
167164 )
168165 )
169166 }
170167 }
171168
172- val mainActivityIntent = buildIntent< MainActivity >( this ) {
173- flags = Intent . FLAG_ACTIVITY_CLEAR_TOP
174- }
175- val mainPendingIntent =
176- PendingIntent .getActivity( this , 0 , mainActivityIntent, PendingIntent . FLAG_ONE_SHOT )
177- builder.setContentIntent(mainPendingIntent )
169+ builder.setContentIntent(
170+ PendingIntent .getActivity(
171+ this , 0 , packageManager.getLaunchIntentForPackage(packageName),
172+ PendingIntent . FLAG_IMMUTABLE
173+ )
174+ )
178175
179176 manager.notify(notificationUtils.incrementAndGetId(), builder.build())
180177 }
0 commit comments