Skip to content

feat: push notifications new controller#23137

Merged
Prithpal-Sooriya merged 39 commits intodevelopfrom
feature/install-and-use-fcm-sw
Apr 26, 2024
Merged

feat: push notifications new controller#23137
Prithpal-Sooriya merged 39 commits intodevelopfrom
feature/install-and-use-fcm-sw

Conversation

@matteoscurati
Copy link
Copy Markdown
Contributor

@matteoscurati matteoscurati commented Feb 22, 2024

Description

This PR introduces a new SW (Service Worker) and a new controller for managing push notifications.

This integration, due to the differences between MV2 and MV3 regarding the use of background scripts and the SW, is only related to the manifest V2. For the manifest V3, part of this logic will be modified, using the extension's own service worker as a starting point. With manifest V2, on the contrary, it is necessary to initialize a dedicated service worker so that FCM (Firebase Cloud Messaging) can handle messages received in the background.

The final UI will allow the user to enable/disable the receipt of notifications. However, this PR concerns only the

new controller and the build management for the creation of the SW.

The new controller will work in accordance with two other controllers for managing authentication and for managing the triggers necessary to receive notifications.

Related issues

No related issues.

Fixes:

Manual testing steps

At the moment, the necessary environment variables have not been inserted into the build process. To test the branch locally, feel free to ask me for the env to insert into your local .metamaskrc file

Screenshots/Recordings

In this video, I show the interaction between a test server for sending notifications and the extension:

https://www.loom.com/share/702fc6a8aea74510acdb1bfac614fa4a

Before

After

Check the entire flow here https://www.figma.com/file/c7GgNw2kScGrVyRGAPhwEd/Notifications?type=design&node-id=484%3A44713&mode=design&t=QsKrmdqZnx42IrMt-1

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've clearly explained what problem this PR is solving and how it is solved.
  • I've linked related issues
  • I've included manual testing steps
  • I've included screenshots/recordings if applicable
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.
  • I’ve properly set the pull request status:
    • In case it's not yet "ready for review", I've set it to "draft".
    • In case it's "ready for review", I've changed it from "draft" to "non-draft".

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

@github-actions
Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@matteoscurati matteoscurati added the team-notifications-deprecated DEPRECATED: please use "team-assets" instead label Feb 22, 2024
@matteoscurati matteoscurati force-pushed the feature/install-and-use-fcm-sw branch from ea7d9a4 to ddb0703 Compare March 4, 2024 11:21
@matteoscurati matteoscurati marked this pull request as ready for review March 5, 2024 13:42
@matteoscurati matteoscurati requested review from a team and kumavis as code owners March 5, 2024 13:42
@matteoscurati matteoscurati marked this pull request as draft March 5, 2024 13:42
@matteoscurati matteoscurati changed the title Feature/install and use fcm sw Feature - Push Notifications New Controller Mar 6, 2024
@matteoscurati matteoscurati changed the title Feature - Push Notifications New Controller feat: push notifications new controller Mar 6, 2024
@gauthierpetetin gauthierpetetin added the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Mar 6, 2024
@@ -0,0 +1,2735 @@
/* eslint-disable */
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@matteoscurati could you include a link to where you copied the code from in this code comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. I'll include the license and the source of these libraries 👍

Copy link
Copy Markdown
Member

@rekmarks rekmarks Apr 1, 2024

Choose a reason for hiding this comment

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

Can we not use the npm package for this? https://www.npmjs.com/package/firebase

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Great question. Unfortunately, Lavamoat node doesn't support ESM, and Firebase does not expose an entry point in CJS. For this reason, at least for now, we had to import Firebase in the way indicated. If I understand correctly, this problem will be solved with the release of Endomoat

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we transpile Firebase using Babel instead?

There is an example of how we did this in the past here: https://github.com/MetaMask/metamask-extension/pull/17251/files#diff-a05d1f077953d24c0d8ddecf64163da2653ba3b728e1ba1f18af6d770767b127R831-R841

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks to @FrederikBolding 's suggestion, I'm able to import Firebase using npm instead of importing the libraries directly!

@@ -0,0 +1,4 @@
/* eslint-disable */
Copy link
Copy Markdown
Contributor

@danjm danjm Mar 27, 2024

Choose a reason for hiding this comment

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

A few questions/suggestions:

  • why does this file need to be minified?
  • is it copied from somewhere? if so where? and then does a license need to be included?
  • can we include a comment at the top of this file explaining what it is?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same for the below file

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, see the comment above.

The library is minified because it is published that way here https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-sw.js.

In any case, I have proceeded to expand it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure. See the comment above.

The library is minified because it is published that way here https://www.gstatic.com/firebasejs/10.8.0/firebase-messaging-sw.js.

In any case, I have proceeded to expand it.

decimalPlaces: 4,
};

export function calcTokenAmount(value: string, decimals: string) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I see you added a code comment for the same function in shared/lib/transactions-controller-utils.js. Did you intend to import that function here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm duplicating this function to keep the SW as light as possible. This way, I don't have to import the entire util file. I know duplication isn't a desirable pattern, but our SW is already quite heavy, and I would like to avoid unnecessary imports.

I added a comment directly in the file!

ERC1155_RECEIVED = 'erc1155_received',
}

const chains = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: because we refer to ETHEREUM as MAINNET throughout the shared/constants/network file, and that is used throughout the codebase, it might be best to name this chains.ETHEREUM property as chains.MAINNET

},
};

export class PushPlatformNotificationsController extends BaseController<
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you add a brief code comment that describes the purpose of the controller?

@@ -0,0 +1,300 @@
/**
* This file was auto-generated by openapi-typescript.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps include one more line here on how to generate the file, maybe even just the specific command that was used?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yep, in fact this was in a previous PR so will remove this and make sure the generated file is documented

public async enablePushNotifications(UUIDs: string[]) {
const bearerToken = await this.getBearerToken();
if (!bearerToken) {
log.error('Failed to enable push notifications: BearerToken token is missing.');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any reason not to include the log.error message in the new Error thrown below? I think that with the current implementation, Sentry won't capture the specific error message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, you're right. I've corrected it using this pattern:

      const errorMessage = `Failed to update triggers for push notifications: ${error}`;
      log.error(errorMessage);
      throw new Error(errorMessage);

Can it work?

danjm

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

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

Okay, I have completed a first review of this PR. Looks really good overall. My comments are relatively minor

@matteoscurati matteoscurati force-pushed the feature/install-and-use-fcm-sw branch from 4744f9f to b660747 Compare March 28, 2024 15:51
@matteoscurati
Copy link
Copy Markdown
Contributor Author

@danjm thank you very much!

I've reviewed your comments and modified the code as requested. I hope everything is ok.

If so, I'll wait to follow the order for merging the PRs we discussed to:

  • change the status of the PR from draft to ready to be merged
  • fix the build tests

@matteoscurati matteoscurati force-pushed the feature/install-and-use-fcm-sw branch from f52d1fd to efbb9b3 Compare April 9, 2024 08:59
@matteoscurati matteoscurati force-pushed the feature/install-and-use-fcm-sw branch 2 times, most recently from 46b861d to 162355b Compare April 11, 2024 20:55
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [8e304f6]
Page Load Metrics (1138 ± 635 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint682061063115
domContentLoaded9291753
load55320711381323635
domInteractive9291753
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 159.96 KiB (4.42%)
  • ui: 378 Bytes (0.01%)
  • common: 2.62 KiB (0.04%)

@matteoscurati matteoscurati requested a review from danjm April 24, 2024 07:41
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [51a7968]
Page Load Metrics (1597 ± 725 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint702211334521
domContentLoaded15101342211
load58376915971510725
domInteractive15101342211
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 159.96 KiB (4.42%)
  • ui: 378 Bytes (0.01%)
  • common: 2.62 KiB (0.04%)

delete flatState.vault;

// The FCM should not be exposed to the UI
delete flatState.fcmToken;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we can avoid having to do this. If the PushPlatformNotificationsController is added to this.store in the metamask-controller but not this.memStore, then it's state won't be exposed to the UI, but it will still be persisted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks for the comment, now everything is much clearer. Removed. Thx!

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [f8a4b25]
Page Load Metrics (1613 ± 675 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint712471285024
domContentLoaded107129178
load59322516131405675
domInteractive107129178
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 159.94 KiB (4.42%)
  • ui: 378 Bytes (0.01%)
  • common: 2.62 KiB (0.04%)

Copy link
Copy Markdown
Contributor

@Prithpal-Sooriya Prithpal-Sooriya left a comment

Choose a reason for hiding this comment

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

Some comments - will resolve them

remove duplicate files and files that should not impact this build
Comment on lines +36 to +44
export const CHAIN_SYMBOLS = {
[NOTIFICATION_CHAINS.ETHEREUM]: 'ETH',
[NOTIFICATION_CHAINS.OPTIMISM]: 'ETH',
[NOTIFICATION_CHAINS.BSC]: 'BNB',
[NOTIFICATION_CHAINS.POLYGON]: 'MATIC',
[NOTIFICATION_CHAINS.ARBITRUM]: 'ETH',
[NOTIFICATION_CHAINS.AVALANCHE]: 'AVAX',
[NOTIFICATION_CHAINS.LINEA]: 'ETH',
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hardcoded Chain Symbols since we need Platform agnostic values (not tied to extension).

If there are shared values then sweet, otherwise we'll keep as is for now.

this isn't really needed - we can add a feature comment once everything is merged
Copy link
Copy Markdown
Contributor

@Prithpal-Sooriya Prithpal-Sooriya left a comment

Choose a reason for hiding this comment

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

LGTM! We will wait for an extension review (and also lets merge this after next RC to avoid additional MV3 debugging)

Copy link
Copy Markdown
Contributor

@danjm danjm left a comment

Choose a reason for hiding this comment

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

LGTM

@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [b0e6312]
Page Load Metrics (1958 ± 806 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint723031705727
domContentLoaded10138483718
load57403019581678806
domInteractive10138483718
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 159.24 KiB (4.43%)
  • ui: 0 Bytes (0.00%)
  • common: 2.17 KiB (0.04%)

@Prithpal-Sooriya Prithpal-Sooriya merged commit f6728c8 into develop Apr 26, 2024
@Prithpal-Sooriya Prithpal-Sooriya deleted the feature/install-and-use-fcm-sw branch April 26, 2024 10:35
@github-actions github-actions bot removed the needs-dev-review PR needs reviews from other engineers (in order to receive required approvals) label Apr 26, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Apr 26, 2024
@metamaskbot
Copy link
Copy Markdown
Collaborator

Builds ready [a0dc43f]
Page Load Metrics (190 ± 269 ms)
PlatformPageMetricMin (ms)Max (ms)Average (ms)StandardDeviation (ms)MarginOfError (ms)
ChromeHomefirstPaint57137772010
domContentLoaded96815147
load462635190561269
domInteractive96715147
Bundle size diffs [🚨 Warning! Bundle size has increased!]
  • background: 159.24 KiB (4.42%)
  • ui: 0 Bytes (0.00%)
  • common: 2.17 KiB (0.04%)

@gauthierpetetin gauthierpetetin added the release-12.0.0 Issue or pull request that will be included in release 12.0.0 label Jun 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-12.0.0 Issue or pull request that will be included in release 12.0.0 team-notifications-deprecated DEPRECATED: please use "team-assets" instead

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants