Push notification workflow
Implement Web Push Notifications using the service worker and Firebase Messaging
These are the exact same native notifications received on mobile phones, home screen, or desktop but they are triggered via a web application. Your application must have the Service Worker installed to implement push notification. See the PWA workflow guide for implementing the Service Worker.
Update the User interface with a tokens property
File: libs\models\src\lib\user\user.interface.ts
In order to send push notifications, it is required to have a User document available in our Firestore database that matches the User's uid .
When a User subscribes to push notifications, the User document will be updated with the token.
Update the project's Web Push certificates
Visit the Project Settings for your project in the Firebase Console: https://console.firebase.google.com/
Select the Cloud Messaging tab.

Generate a key pair.

The public key is used as a unique server identifier for subscribing the user to notifications sent by that server.
The private key needs to be kept secret (unlike the public key) and its used by the application server to sign messages, before sending them to the Push Service for delivery.
Once we have the keys generated we can now use the public key to subscribe to Push Notifications using the Service Worker.
Update app manifest
The application manifest file needs to be updated with the new messaging id.
Manifest file: apps\{{project}}\src\manifest.webmanifest
Update to include the following:
The gcm_sender_id is used by some browsers to enable push notifications. It is the same for all projects, this is not your project's sender ID. Reference
Add the Firebase Messaging service worker
The Firebase Messaging service worker listens for messages to act upon.
Create the file: apps\{{project}}\src\firebase-messaging-sw.js
Note: the file must be named exactly as above.
Add the following code to the file:
Verify the latest firebase package versions from the official example.
Update messagingSenderId with the project's Sender ID, found in the Firebase Console project setting's Cloud Messaging tab.

Update project.json
Update apps\{{project}}\project.json to include the new service worker file asset.
Create the Firebase Messaging service
Run the below command to create the Firebase Messaging service library:
The following files will be generated:
Delete the services-firebase-messaging.module.ts file.
Update the index.ts file to remove the module export.
Run the below command to create the Firebase Messaging Service
Update the index.ts file to export the firebase messaging service file.
Update the service with the following code:
Prepare the Account state for managing the tokens
User tokens will be managed on the User document in the Firestore database. Therefore, we will need to update the AccountState to handle the new token management process.
Update Account actions
File: libs\features\account\src\lib+state\account.actions.ts
New actions:
Update Account reducer
File: libs\features\account\src\lib+state\account.reducer.ts
Add the updateAccountTokensFailure
Update Account selectors
File: libs\features\account\src\lib+state\account.selectors.ts
New selector:
Update Account facade
File: libs\features\account\src\lib+state\account.facade.ts
Update Account effects
File: libs\features\account\src\lib+state\account.effects.ts
New effect:
The updateAccountTokens$ effect manages whether or not we should send a request to the backend to update the User's tokens.
If the token already exists in the User document, a request will not be sent. However, if the User has created a new subscription (could be a new device, refreshed token subscription, or they uninstalled/reinstalled the app) a request will be made to the backend to update the User document.
This is an important step to reduce costs.
Update Account service
File: libs\services\account\src\lib\account.service.ts
Add the new method that will interact with the Cloud Function that updates the User document with the latest tokens. Interacting with a Cloud Function, rather than allowing the client to update documents in the database directly, is the preferred and secure way to interact with the database.
Create Cloud Function to manage new tokens
Since the tokens are stored in the User document, a new Account Cloud Function is needed to update the document.
In the account functions folder, create a on-update-account-tokens.ts file.
Location: apps\full-stack-typescript-api\src\functions\account
Update the index.ts file to export the new function.
File: apps\full-stack-typescript-api\src\functions\account\index.ts
Update the main.ts file to include the new function.
File: apps\full-stack-typescript-api\src\main.ts
Decide when to request and manage tokens
Push notifications are only sent when we have an Authenticated user and that user's document (account details) has been loaded. There are many strategies to decide on when to manage the user tokens.
The Full Stack Typescript repository uses the app-toolbar as a strategy to help manage this process. The app-toolbar receives authentication and account inputs to emit output events for loading and managing account information. It is achieved by the following:
File: libs\components\app-toolbar\src\lib\app-toolbar.component.ts
Update the App module
The account feature should be moved to an eagerly loaded strategy in order to access the state immediately.
File: apps\full-stack-typescript\src\app\app.module.ts
Update the imports to include the AccountModule
Update the App component
The app.component manages the dispatching of actions and retrieving information from the store to communicate with the app-toolbar
File: apps\full-stack-typescript\src\app\app.component.ts
Update to include:
Update the App template
The app-toolbar emits events to communicate to the parent component when to dispatch actions such as loading the user account details, prompting the user to subscribe to notifications, and loading statuses.
File: apps\full-stack-typescript\src\app\app.component.html
Deploy latest changes
Push notifications can be tested through localhost . Build the latest Cloud Function updates and deploy to Firebase to test the push notifications.
Run: nx build {{api project}}
Example: nx build full-stack-typescript-api
After a successful build, run:
firebase deploy --only functions
Subscribe to push notifications
Run: nx serve {{project}}
Example: nx serve full-stack-typescript
Navigate to http://localhost:4200
Login as a user
When prompted click Allow to subscribe to push notifications
Test push notifications
Navigate to Messaging in the Firebase cloud console:
Firebase console: https://console.firebase.google.com/

Click Create your first campaign

Select Firebase Notification messages and Click Create

Enter a Notification title and Notification text then Click Send test message

Add your subscription token and click Test

You should receive a push notification on the device you subscribed with.
Last updated