is it posible to config web-push to send notification to a group of users not all of them

Viewed 331

In our project we need to send notification to many users (even the app is not open in the browser) Also some times we should send a specific notification just for a group of users not all of them. And i know, I can do it with firebase cloud message I know a little about web-push module that use fcm itself, but I am wondering if I can use it instead of fcm?is it posible to config web-push to send notification to a group of users or sending notification for all users in on call? Can anyone help me in this regard and let me know about the main diffrence between fcm and web-push and which is better or I should use? thanks in advance

//using web-push to notify //back-end with nod.js:

const webpush = require('web-push');

const vapidKeys = {
publicKey: '\<My_PUBLIC_KEY\>',
privateKey: '\<My_PRIVATE_KEY\>'
};

// get client subscription config from db
const subscription = {
endpoint: '',
expirationTime: null,
keys: {
auth: '',
p256dh: '',
},
};

const payload = {
notification: {
title: 'Title',
body: 'This is my body',
icon: 'assets/icons/icon-384x384.png',
actions: \[
{ action: 'bar', title: 'Focus last' },
{ action: 'baz', title: 'Navigate last' },
\],
data: {
onActionClick: {
default: { operation: 'openWindow' },
bar: {
operation: 'focusLastFocusedOrOpen',
url: '/',
},
baz: {
operation: 'navigateLastFocusedOrOpen',
url: '/',
},
},
},
},
};

const options = {
vapidDetails: {
subject: '',
publicKey: vapidKeys.publicKey,
privateKey: vapidKeys.privateKey,
},
TTL: 60,
};

// send notification
webpush.sendNotification(subscription, JSON.stringify(payload), options)
.then((_) =\> {_
console.log('SENT!!!');
console.log();
})
.catch((_) =\> {_
console.log();
});
1 Answers

I use fcm for my notifications as it allows me to send my notifications to an array containing multiple user notification token:

const axios = require('axios');

async function sendNotification({
  token,
  sender,
  requestId,
  amount,
}) {
  try {
    const data = {
      notification: {
        body: `Hi, Please confirm payment of ${amount} from ${sender} to be charged from your account`,
        title: 'Account request',
        click_action: 'Account_request',
      },
      data: {
        sender_phone_number: sender,
        amount,
        body: 'Test',
        title: 'MyMoney',
        request_id: requestId,
        category: 'Account_request',
      },
      to: token,
      sound: 'default',
      category: 'Account_request',
    };

    const resp = await axios.post('https://fcm.googleapis.com/fcm/send', data, {
      headers: {
        Authorization: 'key=AuthorizationKey',
      },
    });

    // If notification failed, log error
    if (resp.data.failure) {
      throw new Error('Failed to send notification');
    }
  } catch (error) {
    console.error(error);
  }
}

module.exports = {
  sendNotification
};

The token can be:

const token = ["firebaseToken1", "firebaseToken2", "firebaseToken3"]
Related