Meteor Push Notifications
Enable Push Notifications for Meteor Apps.
Installation
Add the package to your project:
meteor add xerdi:push-notifications
Usage
Every client which want to receive incoming push notifications must firstly register and after that subscribe to the PushNotifications
service.
1import {PushNotifications} from 'meteor/xerdi:push-notifications'; 2 3PushNotifications.registerAndSubscribe(); 4 5// OR 6 7await PushNotifications.register(); 8await PushNotifications.subscribe();
Then on the server every subscribed client can be notified like:
1import {VAPIDCredentials, PushNotifications} from 'meteor/xerdi:push-notifications'; 2 3Meteor.startup(function () { 4 // Sets the credentials in any possible way 5 VAPIDCredentials.auto().init(); 6 // VAPIDCredentials.fromSecrets().init(); 7 // VAPIDCredentials.fromEnv().init(); 8 // VAPIDCredentials.fromSettings().init(); 9 10 // Get the connection info of a users subscription (required) 11 const user = Meteor.users.findOne({}); 12 const {subscriptions} = user.services.notifications; 13 for (let sub of subscriptions) { 14 PushNotifications.sendNotification(sub, { 15 title: 'Test notification', 16 opts: { 17 icon: '/icons/my-icon.png', 18 body: 'Hello World!' 19 } 20 }); 21 } 22});
API
PushNotifications
The PushNotifications
class has the following members and methods:
registration : ServiceWorkerRegistration
The service worker registration.subscription : PushSubscription
The push subscription.status
Eitheruninitialized
,unsupported
,registered
orsubscribed
.notifications()
Gets the notifications of the current registration.registerAndSubscribe()
Both registers and subscribes to push notifications.register()
Registers a service worker.subscribe()
Enables push notifications for the current user.unsubscribe()
Destroys both the registration and subscriptions. This method has to be called beforeAccounts#onLogout
, since it has to be logged in for it to work.sendNotification(sub, notification)
Pushes a notification for the given subscription.requestPermission()
Requests browser permission for receiving push notifications.
VAPIDCredentials
The VAPIDCredentials
class has the following static factory methods:
#auto()
Tries all underlying methods in the exact same order.#fromSecrets()
Tries to get the values from Docker secrets under/run/secrets
. Allowed keys arevapid-public-key
,vapid-private-key
andvapid-contact-uri
.#fromEnv()
Tries to get the values fromprocess.env
. Allowed keys areVAPID_PUBLIC_KEY
,VAPID_PRIVATE_KEY
andVAPID_CONTACT_URI
.#fromSettings()
Tries to get the values fromMeteor.settings
. Don't forget to addsettings.json
to your.gitignore
.#create()
Generates credentials withwebpush.generateVAPIDKeys
.
Furthermore, the VAPIDCredentials
has the following members and methods:
.isNewlyCreated : boolean
Whether the credentials are newly created..contactUri : String
The contact URI of this subscription..publicKey : String
The public key..privateKey : String
The private key..init()
Sets the credentials for thewebpush
service..save()
Stores the values in<project_dir>/settings.json
.
Note: the build-plugin.js
will store the credentials automatically if newly created.