Skip to content

Update how the app decides whether to accept a push notification#15222

Merged
irfano merged 11 commits intotrunkfrom
issue/WOOMOB-1957-update-how-decide-whether-accept-pn
Jan 30, 2026
Merged

Update how the app decides whether to accept a push notification#15222
irfano merged 11 commits intotrunkfrom
issue/WOOMOB-1957-update-how-decide-whether-accept-pn

Conversation

@irfano
Copy link
Contributor

@irfano irfano commented Jan 21, 2026

Closes WOOMOB-1957

Description

This PR adds deduplication logic to NotificationMessageHandler to prevent duplicate push notifications. When WOO_PUSH_NOTIFICATIONS_SYSTEM feature flag is enabled, we check if the incoming notification is from WPCOM (note_id > 0) and if the device is already registered with Woo Core (hasPushToken()). If both conditions are true, we skip the WPCOM notification since the same event will also arrive via Woo Core.

Test Steps

  1. Set up a store with the new Woo Core endpoints by following these steps: p91TBi-dyW-p2#comment-14564
  2. Install the app and log into your store.
  3. Submit a product review from the web interface.
  4. Regression: Confirm you receive the notification, because the feature flag is disabled.
  5. Apply this patch to enable the feature flag: Enable_WOO_PUSH_NOTIFICATIONS_SYSTEM_feature_flag_for_debug_builds.patch
  6. Rebuild the app.
  7. Logout and log in again the app to trigger new push token registration. (you may need to kill and restart after login)
  8. Submit another product review.
  9. Verify that you do not receive a notification. (Note: You won't receive it yet because the development plugin is still a work in progress.)

Images/gif

  • I have considered if this change warrants release notes and have added them to RELEASE-NOTES.txt if necessary. Use the "[Internal]" label for non-user-facing changes.

@irfano irfano added this to the 24.0 milestone Jan 21, 2026
@irfano irfano requested a review from JorgeMucientes January 21, 2026 00:07
@irfano irfano added feature: notifications Related to notifications or notifs. Task (new) labels Jan 21, 2026
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 21, 2026

📲 You can test the changes from this Pull Request in WooCommerce-Wear Android by scanning the QR code below to install the corresponding build.
App NameWooCommerce-Wear Android
Platform⌚️ Wear OS
FlavorJalapeno
Build TypeDebug
Commit891dff4
Direct Downloadwoocommerce-wear-prototype-build-pr15222-891dff4.apk

@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Jan 21, 2026

📲 You can test the changes from this Pull Request in WooCommerce Android by scanning the QR code below to install the corresponding build.

App NameWooCommerce Android
Platform📱 Mobile
FlavorJalapeno
Build TypeDebug
Commit891dff4
Direct Downloadwoocommerce-prototype-build-pr15222-891dff4.apk

@codecov-commenter
Copy link

codecov-commenter commented Jan 21, 2026

Codecov Report

❌ Patch coverage is 77.27273% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 38.77%. Comparing base (1308abb) to head (891dff4).
⚠️ Report is 129 commits behind head on trunk.

Files with missing lines Patch % Lines
...d/notifications/push/NotificationMessageHandler.kt 73.33% 0 Missing and 4 partials ⚠️
...merce/android/notifications/push/RegisterDevice.kt 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##              trunk   #15222   +/-   ##
=========================================
  Coverage     38.77%   38.77%           
- Complexity    10611    10613    +2     
=========================================
  Files          2201     2201           
  Lines        125148   125157    +9     
  Branches      17354    17358    +4     
=========================================
+ Hits          48530    48535    +5     
- Misses        71698    71700    +2     
- Partials       4920     4922    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JorgeMucientes JorgeMucientes self-assigned this Jan 21, 2026
@JorgeMucientes
Copy link
Contributor

JorgeMucientes commented Jan 22, 2026

Hey @irfano the code changes look good, however, I think we can do this in a cleaner way once we merge the changes from the PR I'll open tomorrow early morning (I'm wrapping up testing changes): #15238.
With the changes from that PR we can directly use PushNotificationRegistrationStatus class to verify if the user is registered in Woo system. That will avoid having to change the code you added here once we remove the feature flag. In fact relying on PushNotificationRegistrationStatus use case is feature flag agnostic,
Let me know what you think.

@irfano irfano modified the milestones: 24.0, 24.1 Jan 22, 2026
…system

- Bypass user ID and `remoteNoteId` validation checks when `WOO_PUSH_NOTIFICATIONS_SYSTEM` feature flag is enabled.
- Skip WPCOM notifications when the app is registered with Woo Core to avoid duplicates.
@irfano irfano force-pushed the issue/WOOMOB-1957-update-how-decide-whether-accept-pn branch from 823bd27 to 8a42fa9 Compare January 22, 2026 20:44
@irfano irfano changed the base branch from trunk to issue/woomob-2042-add-fallback-to-wpcom-pn-system January 22, 2026 20:44
@irfano irfano marked this pull request as draft January 22, 2026 20:45
@irfano
Copy link
Contributor Author

irfano commented Jan 22, 2026

I've rebased onto your branch. Let's merge yours first since this is smaller. I agree that using PushNotificationRegistrationStatus in this PR is cleaner.

@irfano irfano marked this pull request as ready for review January 27, 2026 15:45
val notification = notificationModel.toAppModel(resourceProvider)
if (notification.remoteNoteId == 0L) {
val isRegisteredForWooPush = runBlocking {
registrationStatus(notification.remoteSiteId) == PushNotificationRegistrationStatus.Status.WOO_REGISTERED
Copy link
Contributor

@JorgeMucientes JorgeMucientes Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check should also include the case were user is registered in both systems, right?

Suggested change
registrationStatus(notification.remoteSiteId) == PushNotificationRegistrationStatus.Status.WOO_REGISTERED
val isRegisteredForWooPush = runBlocking {
val registrationStatus = registrationStatus(notification.remoteSiteId)
registrationStatus == PushNotificationRegistrationStatus.Status.REGISTERED_IN_BOTH
|| registrationStatus == PushNotificationRegistrationStatus.Status.WOO_REGISTERED
}

The most likely case were we get duplicates is when the user is registered in both systems (even though PN should be turned off in WP.com system)

Copy link
Contributor Author

@irfano irfano Jan 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 You're right. I'm confused about WOO_REGISTERED would be true even though it's registered in both. I renamed (82eb608) the enums and added ONLY suffix to avoid any confusion in the future. Does that sound good?
Also fixed the logic: 0f667f4

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don’t follow commits for the review, I’ve synced with the base branch many times since this PR has been open for a while.

Base automatically changed from issue/woomob-2042-add-fallback-to-wpcom-pn-system to trunk January 29, 2026 17:49
@irfano irfano requested a review from JorgeMucientes January 29, 2026 18:17
val notification = notificationModel.toAppModel(resourceProvider)
if (notification.remoteNoteId == 0L) {
val isRegisteredForWooPush = runBlocking {
registrationStatus(notification.remoteSiteId) == PushNotificationRegistrationStatus.Status.REGISTERED_BOTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should still include PushNotificationRegistrationStatus.Status.REGISTERED_WOO_ONLY in this check too.
While it might be a corner case I can see how we can end in a situation were locally, the app thinks the user is only registered in WOO system only, however their site might still be registered in both PN systems in the backend side. For example, if user is registered in WP.com system and fails to unregister device on log out and then logs in again and registers successfully in Woo system but fails to turn off notifications in WP.com.
That's just one case but I think there could be others. To stay on the safe side I'd check either values REGISTERED_BOTH || REGISTERED_WOO_ONLY

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what happened exactly, but when I added this comment to the PR this morning I would've sworn that the PushNotificationRegistrationStatus.Status.REGISTERED_WOO_ONLY was not present in the code. Look like it is now. So all good 👍🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually included PushNotificationRegistrationStatus.Status.REGISTERED_WOO_ONLY and PushNotificationRegistrationStatus.Status.REGISTERED_BOTH after your feedback, but I think I messed it up while resolving conflicts.
I re-added it today in commit 891dff474a77593f0da8181b34a65cd48905144a. That's why I commented "don't follow my commits" 😅
Anyway, thanks for the review.

Copy link
Contributor

@JorgeMucientes JorgeMucientes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Nice work @irfano

Comment on lines 70 to 73
if (!accountStore.hasAccessToken()) {
wooLog.e(NOTIFICATIONS, "User is not logged in!")
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of scope for this PR but we probably need to update this as this condition will be false for all users loggin in with site credentials.
Can you create a nwe Linear task and tackle that @irfano 🙏🏼

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻 Opened: WOOMOB-2113

@irfano irfano merged commit 6461df3 into trunk Jan 30, 2026
19 checks passed
@irfano irfano deleted the issue/WOOMOB-1957-update-how-decide-whether-accept-pn branch January 30, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: notifications Related to notifications or notifs. Task (new)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants