This is a loopback-next extension for adding different notification mechanisms vis-Ã -vis, Push, SMS, Email to any loopback 4 based REST API application or microservice.
It provides a generic provider-based framework to add your own implementation or implement any external service provider to achieve the same. There are 3 different providers available to be injected namely, PushProvider, SMSProvider and EmailProvider. It also provides support for 7 very popular external services for sending notifications.
Pubnub - It's one of the PushProvider for sending realtime push notifications to mobile applications as well as web applications.
Socket.IO - It's one of the PushProvider for sending realtime push notifications to mobile applications as well as web applications.
FCM - It's one of the PushProvider for sending realtime push notifications to mobile applications as well as web applications.
Nodemailer - It's one of the EmailProvider for sending email messages.
Apple Push Notification service - It's one of the push notification providers that integrates notification service created by Apple Inc. that enables third party application developers to send notification data to applications installed on Apple devices.
Twilio SMS Service - Twilio is a modern communication API Used by developers for establishing communications. Twilio can be used for sending SMS or Whatapp notifications.
You can use one of these services or add your own implementation or integration using the same interfaces and attach it as a provider for that specific type.
You can use one of these services or add your own implementation or integration using the same interfaces and attach it as a provider for that specific type.
After the above, you need to configure one of the notification provider at least. Based upon the requirement, please choose and configure the respective provider for sending notifications. See below.
Email Notifications using Amazon Simple Email Service¶
This extension provides in-built support of AWS Simple Email Service integration for sending emails from the application. In order to use it, run npm install aws-sdk, and then bind the SesProvider as below in application.ts.
There are some additional configurations needed in order to allow SES to connect to AWS. You need to add them as below. Make sure these are added before the provider binding.
Possible configuration options for the above are mentioned below.
Option
Type
Description
sendToMultipleReceivers
boolean
If set to true, single email will be sent to all receivers mentioned in payload. If set to false, multiple emails will be sent for each receiver mentioned in payload.
senderEmail
string
This will be used as from email header in sent email.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to SesProvider we have. Add that provider in place of SesProvider. Refer to the implementation here.
This extension provides in-built support of Nodemailer integration for sending emails from the application. In order to use it, run npm install nodemailer, and then bind the NodemailerProvider as below in application.ts.
There are some additional configurations needed in order to allow NodeMailer to works. You need to add them as below. Make sure these are added before the provider binding.
Possible configuration options for the above are mentioned below.
Option
Type
Description
sendToMultipleReceivers
boolean
If set to true, single email will be sent to all receivers mentioned in payload. If set to false, multiple emails will be sent for each receiver mentioned in payload.
senderEmail
string
This will be used as from email header in sent email.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to NodemailerProvider we have. Add that provider in place of NodemailerProvider. Refer to the implementation here.
This extension provides in-built support of AWS Simple Notification Service integration for sending SMS from the application. In order to use it, run npm install aws-sdk, and then bind the SnsProvider as below in application.ts.
There are some additional configurations needed in order to allow SNS to connect to AWS. You need to add them as below. Make sure these are added before the provider binding.
All the configurations as specified by AWS docs here are supported in above SNSBindings.Config key.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to SnsProvider we have. Add that provider in place of SnsProvider.Refer to the implementation here.
This extension provides in-built support of Twilio integration for sending SMS / whatsapp notifications from the application. In order to use it, run npm install twilio, and then bind the TwilioProvider as below in application.ts.
There are some additional configurations needed in order to connect to Twilio. You need to add them as below. Make sure these are added before the provider binding.
All the configurations as specified by Twilio docs and console are supported in above TwilioBindings Config key. smsFrom could be messaging service id, twilio number or short code. waFrom could be whats app number or number associated to channel.
This extension provides in-built support of Pubnub integration for sending realtime push notifications from the application. In order to use it, run npm install pubnub, and then bind the PushProvider as below in application.ts.
There are some additional configurations needed in order to allow Pubnub connection. You need to add them as below. Make sure these are added before the provider binding.
All the configurations as specified by Pubnub docs here are supported in above PubNubProvider.Config key.
Additionally, PubNubProvider also supports Pubnub Access Manager integration. Refer docs here for details.
For PAM support, PubNubProvider exposes two more methods - grantAccess and revokeAccess. These can be used to grant auth tokens and revoke them from Pubnub.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to PubNubProvider we have. Add that provider in place of PubNubProvider. Refer to the implementation here.
This extension provides in-built support of Socket.io integration for sending realtime notifications from the application. In order to use it, run npm install socket.io-client, and bind the PushProvider as below in application.ts.
This provider sends the message to the channel passed via config (or while publishing) and accepts a fix interface to interact with.
The interface could be imported into the project by the name SocketMessage.
There are some additional configurations needed in order to allow Socket connection. You need to add them as below. Make sure these are added before the provider binding.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to SocketIOProvider we have. Add that provider in place of SocketIOProvider. Refer to the implementation here.
This extension provides in-built support of Firebase Cloud Messaging integration for sending realtime push notifications from the application. In order to use it, run npm i firebase-admin, and then bind the PushProvider as below in application.ts.
There are some additional configurations needed in order to use Firebase Cloud Messaging. You need to add them as below. Make sure these are added before the provider binding.
import{NotificationsComponent,NotificationBindings,}from'loopback4-notifications';import{FcmProvider,FcmBindings}from'loopback4-notifications/fcm';exportclassMyApplicationextendsBootMixin(ServiceMixin(RepositoryMixin(RestApplication)),){constructor(options:ApplicationConfig={}){// ...this.component(NotificationsComponent);this.bind(FcmBindings.Config).to({apiKey:'API_KEY',authDomain:'PROJECT_ID.firebaseapp.com',// The value of `databaseURL` depends on the location of the databasedatabaseURL:'https://DATABASE_NAME.firebaseio.com',projectId:'PROJECT_ID',storageBucket:'PROJECT_ID.appspot.com',messagingSenderId:'SENDER_ID',appId:'APP_ID',// For Firebase JavaScript SDK v7.20.0 and later, `measurementId` is an optional fieldmeasurementId:'G-MEASUREMENT_ID',});this.bind(NotificationBindings.PushProvider).toProvider(FcmProvider);// ...}}
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to FcmProvider we have. Add that provider in place of FcmProvider. Refer to the implementation here.
This extension provides in-built support of Apple Push Notification service for sending notification to applications installed on Apple devices. In order to use it bind the PushProvider as below in application.ts.
There are some additional configurations needed in order to use Apple Push Notification service. You need to add them as below. Make sure these are added before the provider binding.
If you wish to use any other service provider of your choice, you can create a provider for the same, similar to ApnsProvider we have. Add that provider in place of ApnsProvider. Refer to the implementation here.
Once the providers are set, the implementation of notification is very easy. Just add an entity implementing the Message interface provided by the component. For specific type, you can also implement specific interfaces like, SMSMessage, PushMessage, EmailMessage. See example below.
After this, you can publish notification from controller API methods as below. You don't need to invoke different methods for different notification. Same publish method will take care of it based on message type sent in request body.
exportclassNotificationController{constructor(// ...@inject(NotificationBindings.NotificationProvider)privatereadonlynotifProvider:INotification,){}@post('/notifications',{responses:{[STATUS_CODE.OK]:{description:'Notification model instance',content:{[CONTENT_TYPE.JSON]:{schema:getModelSchemaRef(Notification)},},},},})asynccreate(@requestBody({content:{[CONTENT_TYPE.JSON]:{schema:getModelSchemaRef(Notification,{exclude:['id']}),},},})notification:Omit<Notification,'id'>,):Promise<Notification>{awaitthis.notifProvider.publish(notification);}}
As you can see above, one controller method can now cater to all the different type of notifications.
If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket.
If not, go ahead and make one!
All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can.
If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.