From 273fae8c07b60916ff09618a97cba141f6b6fe60 Mon Sep 17 00:00:00 2001 From: Rachel Hyman Date: Sun, 21 Aug 2022 20:27:45 -0500 Subject: [PATCH] notifications: Open to unreads from summary notification Adds an intent to the summary notification. When the summary notification is clicked in the collapsed state, the app will open and reset itself to the unreads screen. It will also switch to the appropriate account if needed. Because the intent flags (Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TOP) apply to both the normal notification and the summary one (and are prefaced with explanatory comments), I moved them to a variable before where both notifications are created. This was manually tested for the following cases, with both the app already being on the correct account and being on a different account: - App open in background on non-unreads screen - App open in background on unreads screen - App not open in background I also made sure that tapping on a notification in the expanded state still routes the user to the correct screen. Fixes #5242. --- .../notifications/NotificationUiManager.kt | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/android/app/src/main/java/com/zulipmobile/notifications/NotificationUiManager.kt b/android/app/src/main/java/com/zulipmobile/notifications/NotificationUiManager.kt index 6b65d5180a1..f40493c8102 100644 --- a/android/app/src/main/java/com/zulipmobile/notifications/NotificationUiManager.kt +++ b/android/app/src/main/java/com/zulipmobile/notifications/NotificationUiManager.kt @@ -288,6 +288,26 @@ private fun updateNotification( }.build() messagingStyle.addMessage(fcmMessage.content, fcmMessage.timeMs, sender) + + // See these sections in the Android docs: + // https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes + // https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP + // + // * From the doc on `PendingIntent.getActivity` at + // https://developer.android.com/reference/android/app/PendingIntent#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int) + // > Note that the activity will be started outside of the context of an + // > existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK + // > launch flag in the Intent. + // + // * The flag FLAG_ACTIVITY_CLEAR_TOP is mentioned as being what the + // notification manager does; so use that. It has no effect as long + // as we only have one activity; but if we add more, it will destroy + // all the activities on top of the target one. + // + // * These flags get created up here so that we can use them for both + // the notification and the summary notification. + val intentFlags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP + val notification = NotificationCompat.Builder(context, CHANNEL_ID).apply { setGroup(groupKey) @@ -323,23 +343,7 @@ private fun updateNotification( setContentIntent( PendingIntent.getActivity(context, 0, Intent(Intent.ACTION_VIEW, intentUrl, context, MainActivity::class.java) - .setFlags( - // See these sections in the Android docs: - // https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes - // https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP - // - // * From the doc on `PendingIntent.getActivity` at - // https://developer.android.com/reference/android/app/PendingIntent#getActivity(android.content.Context,%20int,%20android.content.Intent,%20int) - // > Note that the activity will be started outside of the context of an - // > existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK - // > launch flag in the Intent. - // - // * The flag FLAG_ACTIVITY_CLEAR_TOP is mentioned as being what the - // notification manager does; so use that. It has no effect as long - // as we only have one activity; but if we add more, it will destroy - // all the activities on top of the target one. - Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP - ) + .setFlags(intentFlags) .putExtra(EXTRA_NOTIFICATION_DATA, bundleOf(*fcmMessage.dataForOpen())), PendingIntent.FLAG_IMMUTABLE)) setAutoCancel(true) @@ -361,7 +365,11 @@ private fun updateNotification( // (See example in the linked doc.) ) - // TODO Does this do something useful? There isn't a way to open these summary notifs. + setContentIntent( + PendingIntent.getActivity(context, 0, + Intent(context, MainActivity::class.java) + .setFlags(intentFlags), + PendingIntent.FLAG_IMMUTABLE)) setAutoCancel(true) }.build()