Full Guide: How to Add Push Notifications in Angular App?

Latitude Technolabs
5 min readJun 29, 2021

--

Full Guide: How to Add Push Notifications in Angular App?

If you want to implement push notifications in an Angular app from a Web app like you receive notification on your mobile phones and desktop, this article is for you.

This guide will see a full example of enabling or implementing web push notifications in angular app. We will use service workers. We all know that push notifications are a great way to engage users and keep them interested in-app. You will also know how push notification works. So, let’s start it!

Experts recommend going for the best and cost-friendly angular development services for problem-free angular app development, especially startups or running into a problem.

You can get in depth info about how to install and configure Angular PWA before enabling push notifications in angular app.

What Are the Push Notifications?

Web push notifications are depending on two standards, Push API and Notifications API. Push API allows messages to be pushed from servers, and Notifications API displays systems notifications.

But the critical point is, we cannot send push notifications directly from any server to the browser. Only some browser development companies like Mozilla and Google choose and can push notifications on the browser.

Browser Push Service is a server that can send push notifications to browsers. Here you have to know that Mozilla and Google use different Push Services. Both push services are under the control of the parent company.

Browser Push Service Providers

We all know that notifications are sometimes very frustrating for users. So, browser implementers want to ensure that users must have a good user experience.

It clearly means that browser push service providers can block some notifications from being received by users. For example, if notifications are too much and frequent.

For example, all push messages come to firebase cloud messaging of Google Chrome and then receive it. So, it means you cannot send push notifications directly from your end. Here firebase cloud messaging acts as the chrome browser push service. The push service cannot be changed by the browser. The development company of browser manages the push service

So, suppose you want to deliver a push notification to a particular user. In that case, the push service identifies the users in a unique way for protecting user privacy. Also, the push service cannot read the messages; hence they are encrypted.

related article : Best Tips To Improve AngularJS Performance Quickly

Why is it necessary to identify your server as a Push source?

Before starting, you have to identify your server to some browser push services.

Now, each Push service first analyzes the patterns of messaging to avoid spamming notifications. It’s good to identify your servers with push service from time to time to increase the chances of sending push notifications to users from your end.

Let’s start identifying your app first using the VAPID key pair. You must first construct it before utilizing it.

How to Generate a VAPID key pair?

You can generate it using the webpush library. First, you will have to install the webpush library from CLI.

npm install web-push -g

now use the following code to generate the VAPID key pair.

web-push generate-vapid-keys — json

The Key will look like the below image.

Now, you can use the Key to subscribe to push notifications using an angular service worker.

How to subscribe to push notifications?

Here, you will need an angular service worker. After adding an angular service worker to the existing angular application, you can now ask for permission to send push notifications.

02.ts hosted by GitHub

  • If the User clicks on subscribe button, the subscribeToNotifications() method will start.
  • You can ask users using swPush if they want to receive web push messages.
  • The requestSubscription() again asks if the User wants to receive notifications.
  • The User will see a window like the below image.
  • If the User accepts to revive the notification, then a push subscription object .then() is passed.

Allow/Deny Notifications Popup

If you are testing on localhost, you can experience mistakenly hitting on the wrong notification popup. It will not ask again to ask you for notification permission. So, you will have to do something to pop up the display again to give correct permission again.

So, here is the way to do it in order to have the popup displayed again.

  • First of all, go to chrome://settings/content/notifications
  • Now you can see the website list that is blocked and allowed to receive notifications from.
  • Now, delete localhost from the block list and click on subscribe again.

Now a popup will appear again. Now, if you click on the allow button, the push subscription object will be generated.

How Does the Push Notification Object Look?

When you receive it in then(), it looks like the below image.

Now let’s break down each block to get more understanding of each object.

  • endpoint: It contains the URL and its public. But, it is unpredictable for the browser push service used by the app server to send notifications.
  • expirationTime: Some notifications have an expiration date. Such messages will not be sent if time has passed. It is useful in authentication code.
  • p256dh: it is encryption to your server. It is used for encrypting messages before sending to push service.
  • auth: it is an authentication secret. It may contain a message encryption process.

How to Use the Push Notification Object?

Push notification should be stored somewhere so we can see it later. Here we are sending subscription objects via http request to the backend by NewsletterService. For later use, the subscription object will be stored in a database.

Use Node Backend to send push notifications

Now we can send push messages using webpush that are stored in the database. To send push messages to all subscribers, you have to create a REST endpoint using Express.

04.ts hosted by GitHub

  • In the above image, you have to start the webpush module by passing the VAPID key pair.
  • Now you can see that keys are visible in the code, so it’s very unsafe. You can pass a JSON file containing a key pair via CLI.
  • Now you have to start an express app. You have created an HTTP POST endpoint for /API/newsletter URL.

Push Message Bodybuilding

We must utilize the format specified in the code to display the message correctly for the Angular Service Worker.

The message will not display if you don’t payload one property notification. You can also specify a vibration pattern via vibrate.

Using webpush to send Push Notification

Once everything is ready, you can send a call to webpush.sendNotification().

  • Webpush will encrypt the message using p256dh public Key and auth.
  • VAPID private Key will be used for encryption signs.
  • The message will be sent to the firebase cloud messaging endpoint in the endpoint property.

Conclusion

Now, you can send push notifications easily if you have read the whole article. We only learned about sending push notifications, but the problems can arrive at any point in time. The bigger the project is, the deeper the problems are. Still, you can Hire Top Angular Developers in India to solve complex project ideas easily. Latitude technolabs has quality-oriented developers who are highly experienced in corporate level problems but with cost-friendliness.

--

--

Latitude Technolabs
Latitude Technolabs

Written by Latitude Technolabs

Latitude Technolabs Pvt. Ltd. is a leading service provider with extensive experience in providing IT outsourcing services to enterprises across the globe.

No responses yet