A fully functional Android app built entirely with Kotlin and Jetpack Compose
Add pending intent for opening notification links
TJ Dahunsi8a3a16de2127
diff --git a/core/notifications/build.gradle.kts b/core/notifications/build.gradle.ktsindex 86094c2c8..012c6f3f3 100644--- a/core/notifications/build.gradle.kts+++ b/core/notifications/build.gradle.kts@@ -28,6 +28,7 @@ dependencies {implementation(project(":core:model"))implementation(libs.kotlinx.coroutines.android)+ implementation(libs.androidx.browser)implementation(libs.androidx.compose.runtime)implementation(libs.androidx.core.ktx)diff --git a/core/notifications/src/main/java/com/google/samples/apps/nowinandroid/core/notifications/AndroidSystemNotifier.kt b/core/notifications/src/main/java/com/google/samples/apps/nowinandroid/core/notifications/AndroidSystemNotifier.ktindex 6b4c53ef3..c8f3ed9b1 100644--- a/core/notifications/src/main/java/com/google/samples/apps/nowinandroid/core/notifications/AndroidSystemNotifier.kt+++ b/core/notifications/src/main/java/com/google/samples/apps/nowinandroid/core/notifications/AndroidSystemNotifier.kt@@ -20,12 +20,16 @@ import android.Manifest.permissionimport android.app.Notificationimport android.app.NotificationChannelimport android.app.NotificationManager+import android.app.PendingIntentimport android.content.Contextimport android.content.pm.PackageManager+import android.net.Uriimport android.os.Build.VERSIONimport android.os.Build.VERSION_CODES+import androidx.browser.customtabs.CustomTabsIntentimport androidx.core.app.ActivityCompatimport androidx.core.app.NotificationCompat+import androidx.core.app.NotificationCompat.EXTRA_NOTIFICATION_IDimport androidx.core.app.NotificationCompat.InboxStyleimport androidx.core.app.NotificationManagerCompatimport com.google.samples.apps.nowinandroid.core.model.data.NewsResource@@ -33,6 +37,7 @@ import dagger.hilt.android.qualifiers.ApplicationContextimport javax.inject.Injectimport javax.inject.Singleton+private const val NEWS_NOTIFICATION_REQUEST_CODE = 0private const val NEWS_NOTIFICATION_SUMMARY_ID = 1private const val NEWS_NOTIFICATION_CHANNEL_ID = ""private const val NEWS_NOTIFICATION_GROUP = "NEWS_NOTIFICATIONS"@@ -52,9 +57,7 @@ class AndroidSystemNotifier @Inject constructor(this,permission.POST_NOTIFICATIONS,) != PackageManager.PERMISSION_GRANTED- ) {- return- }+ ) returnval newsNotifications = newsResources.map { newsResource ->newsNotification {@@ -63,7 +66,9 @@ class AndroidSystemNotifier @Inject constructor().setContentTitle(newsResource.title).setContentText(newsResource.content)+ .setContentIntent(newsPendingIntent(newsResource)).setGroup(NEWS_NOTIFICATION_GROUP)+ .setAutoCancel(true)}}val summaryNotification = newsNotification {@@ -80,6 +85,7 @@ class AndroidSystemNotifier @Inject constructor(.setStyle(newsInboxStyle(newsResources, title)).setGroup(NEWS_NOTIFICATION_GROUP).setGroupSummary(true)+ .setAutoCancel(true).build()}@@ -139,3 +145,23 @@ private fun Context.ensureNotificationChannel() {// Register the channel with the systemNotificationManagerCompat.from(this).createNotificationChannel(channel)}++private fun Context.newsPendingIntent(newsResource: NewsResource): PendingIntent? =+ PendingIntent.getActivity(+ this,+ NEWS_NOTIFICATION_REQUEST_CODE,+ // TODO: Read color from material theme to style the chrome custom tab+ // this is currently only readable from composition.+ CustomTabsIntent.Builder()+ .build()+ .apply {+ intent.data = Uri.parse(newsResource.url)+ if (VERSION.SDK_INT < VERSION_CODES.O) {+ intent.putExtra(+ EXTRA_NOTIFICATION_ID,+ newsResource.id.hashCode(),+ )+ }+ }.intent,+ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,+ )