0

App.ts

const VAPID_PUBLIC = "XXX"

export class AppComponent {
  title = 'web';

  constructor(swPush: SwPush, pushService: PushNotificationService) {
    if (swPush.isEnabled) {
      swPush
        .requestSubscription({
          serverPublicKey: VAPID_PUBLIC,
        })
        .then(subscription => {
          pushService.sendSubscriptionToTheServer(subscription).subscribe(change => {

          })
        })
        .catch(console.error)
    }
  }

push-notification.service.ts

import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'

const SERVER_URL = 'http://localhost:3000/subscription'

@Injectable()
export class PushNotificationService {
  constructor(private http: HttpClient) {}

  public sendSubscriptionToTheServer(subscription: PushSubscription) {
    return this.http.post(SERVER_URL, subscription)
  }
}

server.js

const express = require('express');
const webpush = require('web-push');
const cors = require('cors');
const bodyParser = require('body-parser');

const PUBLIC_VAPID =
  'XXX';
const PRIVATE_VAPID = 'XXX';

const fakeDatabase = [];

const app = express();

app.use(cors());
app.use(bodyParser.json());

webpush.setVapidDetails('mailto:[email protected]', PUBLIC_VAPID, PRIVATE_VAPID);

app.post('/subscription', (req, res) => {
  const subscription = req.body;
  fakeDatabase.push(subscription);
});

app.post('/sendNotification', (req, res) => {
  const notificationPayload = {
    notification: {
      title: 'New Notification',
      body: 'This is the body of the notification',
    },
  };

  const promises = [];
  fakeDatabase.forEach(subscription => {
    promises.push(
      webpush.sendNotification(
        subscription,
        JSON.stringify(notificationPayload)
      )
    );
  });
  Promise.all(promises).then(() => res.sendStatus(200));
});

app.listen(3000, () => {
  console.log('Server started on port 3000');
});
  • With the above code, I'm trying to display notifications using angular and express. I started the server on http://localhost:3000 then ran ng build --prod then http-server dist/apps/projectName.

  • The request for notifications is working whether to allow or not, on clicking allow, I get response 200 in (checking in network tab chrome dev tools). But the notification message is not getting popped up or displayed in the browser. I need help in fixing this.

I tried the code from https://malcoded.com/posts/angular-push-notifications/

Thank you.

1 Answer 1

1

Looks like you forgot to add a popup message:

pushService.sendSubscriptionToTheServer(subscription).subscribe(change => {

    //-->Here you need to write popup message after notification

})
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, is there any syntax for that?
As such there is no syntax but you may use alert or customized way of showing message box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.