Skip to content

Commit cb4afc7

Browse files
committed
StudyBuddyMessagingService: Remove SettingsActivity dependency
* Introduce settings deep-link for the messaging service to use * Add top-level file for deep-link URIs
1 parent ca51f8f commit cb4afc7

File tree

3 files changed

+46
-44
lines changed

3 files changed

+46
-44
lines changed

app/src/main/java/com/edricchan/studybuddy/navigation/compat/CompatNavigation.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,6 @@ fun NavGraphBuilder.compatGraphs() {
108108
action = Intent.ACTION_APPLICATION_PREFERENCES
109109
}
110110
}
111+
deepLink(UriSettings)
111112
}
112113
}

app/src/main/java/com/edricchan/studybuddy/providers/fcm/StudyBuddyMessagingService.kt

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import android.os.Build
99
import android.util.Log
1010
import androidx.core.app.NotificationCompat
1111
import androidx.core.app.NotificationManagerCompat
12+
import androidx.core.app.TaskStackBuilder
13+
import androidx.core.content.res.ResourcesCompat
14+
import androidx.core.net.toUri
1215
import coil.imageLoader
1316
import coil.request.ImageRequest
1417
import com.edricchan.studybuddy.R
1518
import com.edricchan.studybuddy.constants.Constants
19+
import com.edricchan.studybuddy.core.compat.navigation.UriSettings
1620
import com.edricchan.studybuddy.core.resources.notification.AppNotificationChannel
1721
import com.edricchan.studybuddy.exts.android.buildIntent
1822
import com.edricchan.studybuddy.exts.common.TAG
1923
import com.edricchan.studybuddy.interfaces.NotificationAction
2024
import com.edricchan.studybuddy.ui.modules.main.MainActivity
21-
import com.edricchan.studybuddy.ui.modules.settings.SettingsActivity
2225
import com.edricchan.studybuddy.ui.theming.dynamicColorPrimary
2326
import com.edricchan.studybuddy.utils.NotificationUtils
2427
import 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
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.edricchan.studybuddy.core.compat.navigation
2+
3+
/** Uri for a settings deep-link. */
4+
const val UriSettings = "studybuddy://settings"

0 commit comments

Comments
 (0)