Skip to content

Commit

Permalink
notifications: Open to unreads from summary notification
Browse files Browse the repository at this point in the history
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 zulip#5242.
  • Loading branch information
rachelhyman committed Aug 22, 2022
1 parent b1eba83 commit 273fae8
Showing 1 changed file with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down

0 comments on commit 273fae8

Please sign in to comment.