Skip to content

fix(notify): open MainActivity when notification is tapped - #225

Merged
ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/notification-click-opens-activity
Aug 20, 2026
Merged

ErikBjare merged 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/notification-click-opens-activity

Conversation

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Problem

Tapping an activity-time alert notification ("Time spent: Work 1h 30m") did nothing. The NotifyWorker.sendNotification() method built notifications without a setContentIntent, which is the Android API for wiring up a tap action.

Fix

Add a PendingIntent that opens MainActivity — the AW dashboard/activity view — when the notification is tapped. Uses FLAG_ACTIVITY_CLEAR_TOP so an already-running instance is brought to the front rather than starting a new one.

BackgroundService already had this pattern for the foreground-service notification (line 207); NotifyWorker was missing it.

Change

 private fun sendNotification(alert: CategoryAlert, ...) {
+    val openIntent = Intent(applicationContext, MainActivity::class.java).apply {
+        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
+    }
+    val pendingIntent = PendingIntent.getActivity(
+        applicationContext, 0, openIntent,
+        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
+    )
     val notification = NotificationCompat.Builder(applicationContext, CHANNEL_ID)
         ...
         .setAutoCancel(true)
+        .setContentIntent(pendingIntent)
         .build()
 }

Fixes #224

Without a setContentIntent, tapping an activity-time alert notification
did nothing. Add a PendingIntent that brings MainActivity to the front
(or starts it), opening the AW dashboard/activity view.

BackgroundService already had this pattern for the foreground-service
notification; NotifyWorker's category-alert notifications were missing it.

Fixes: ActivityWatch#224
@greptile-apps

greptile-apps Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

Greptile Summary

The PR makes activity-time alert notifications open MainActivity when tapped and assigns their PendingIntent a distinct request code.

  • Adds an immutable notification content intent with NEW_TASK and CLEAR_TOP.
  • Uses request code 1 to avoid collisions with the foreground-service and widget intents.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the distinct request code fixes the previously reported PendingIntent collision.

Important Files Changed

Filename Overview
mobile/src/main/java/net/activitywatch/android/workers/NotifyWorker.kt Adds the notification tap action and correctly isolates its PendingIntent identity from existing MainActivity intents.

Reviews (2): Last reviewed commit: "fix(notify): use a distinct PendingInten..." | Re-trigger Greptile

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

CI-green and mergeable — waiting only on a maintainer click.

This PR is ready to merge, but the bot has pull-only access to this repo and can't self-merge — surfacing it here so it isn't lost. The monitoring loop will stop re-flagging it now that this note is posted.

BackgroundService's foreground notification already registers request code 0
for a MainActivity PendingIntent with no launch flags. PendingIntent identity
is (requestCode, Intent.filterEquals) — extras and launch flags are not part
of it, and FLAG_UPDATE_CURRENT replaces only extras. Reusing code 0 therefore
resolved to that existing PendingIntent and silently dropped
FLAG_ACTIVITY_CLEAR_TOP, so tapping an alert could stack a second MainActivity
instead of bringing the existing one forward.

Use request code 1 (0 and 2 are taken by BackgroundService and the widget) and
name the collision in a comment so it does not regress.
@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

Correction to my 02:48 comment: this was not "waiting only on a maintainer click" — an unaddressed Greptile P1 had been sitting on it for 4 days. My earlier comment read the green CI and mergeable state and skipped the open review thread. Apologies for the noise.

The finding was real. BackgroundService.kt:209 already registers request code 0 for a MainActivity PendingIntent, and PendingIntent identity is (requestCode, Intent.filterEquals) — launch flags aren't part of it, and FLAG_UPDATE_CURRENT replaces only extras. So the new notification's FLAG_ACTIVITY_CLEAR_TOP was being silently dropped whenever the foreground service had registered first, and tapping an alert could stack a second MainActivity instead of bringing the existing one forward.

Fixed in 64c933d: request code 1, extracted to a named constant documenting which codes are taken (0 = BackgroundService, 2 = CategoryTimeWidgetUpdater open button). One-line behavioural change; thread resolved.

Re-running CI now — will confirm green before this is actually ready.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

CI green on 64c933d — confirming as promised:

Check Result
Build aw-server-rust pass (1m13s)
Get latest versionCode pass (14s)
Test pass (2m12s)
Test E2E (29) pass (10m54s)

Greptile thread resolved. No open review threads remain. Now genuinely ready for a maintainer merge — the bot is pull-only here.

@TimeToBuildBob

Copy link
Copy Markdown
Contributor Author

@greptileai review

@ErikBjare
ErikBjare merged commit dddc5e6 into ActivityWatch:master Aug 20, 2026
8 checks passed
ErikBjare pushed a commit that referenced this pull request Aug 31, 2026
* fix(notify): open the activity view when an alert is tapped

#225 already attached a MainActivity PendingIntent, so the notification
no longer did nothing — but the extra was missing, so a tap still landed
on dashboard home instead of the Activity drawer destination
(/#/activity/unknown/). Pass EXTRA_OPEN_ACTIVITY_VIEW, honor it on cold
start and onNewIntent (SINGLE_TOP), and share the URL with the drawer
item so the two cannot drift.

* fix(notify): honor activity-view extra after process death

If the process was killed, onCreate restores the previous fragment and
used to return before reading EXTRA_OPEN_ACTIVITY_VIEW, so a notification
tap could land on dashboard home. Replace the restored fragment when the
extra is set, and consume it so a later rotation does not reload the
WebView.

* fix(notify): defer activity-view replace until onResume

onNewIntent runs while MainActivity is still stopped, after
onSaveInstanceState. commit() there throws IllegalStateException when
the user taps a notification on a backgrounded app. Stash the intent
and replace the fragment in onResume, which is also the process-death
restore path.

* fix(notify): handle taps while activity is resumed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clicking notifications does nothing

2 participants