Skip to content

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

Open
TimeToBuildBob wants to merge 2 commits into
ActivityWatch:masterfrom
TimeToBuildBob:fix/notification-click-opens-activity
Open

fix(notify): open MainActivity when notification is tapped#225
TimeToBuildBob wants to merge 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.

  • Adds an explicit MainActivity Intent and immutable activity PendingIntent.
  • Attaches the PendingIntent as the notification's content action.
  • Reuses an existing PendingIntent identity, preventing the requested CLEAR_TOP launch behavior from being reliably applied.

Confidence Score: 4/5

The PendingIntent collision should be fixed before merging because alert taps can still create a duplicate MainActivity instead of bringing the current instance forward.

NotifyWorker requests the same MainActivity PendingIntent identity already owned by BackgroundService, so the newly specified CLEAR_TOP and NEW_TASK behavior is not reliably represented by the returned token.

Files Needing Attention: mobile/src/main/java/net/activitywatch/android/workers/NotifyWorker.kt

Important Files Changed

Filename Overview
mobile/src/main/java/net/activitywatch/android/workers/NotifyWorker.kt Adds the missing notification tap action, but request code 0 collides with BackgroundService's MainActivity PendingIntent and can discard the intended activity flags.

Reviews (1): Last reviewed commit: "fix(notify): open MainActivity when noti..." | 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.

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

1 participant