Meteor One-Signal integration
Meteor package to enable push notifications with One-Signal.
Installation
meteor add matheusccastro:one-signal-push-notification
Requirements
- Make sure you created your
One-Signalaccount and have it set up. - Add the following variables in your
Meteor.settingsfile:ONESIGNAL_APP_IDinMeteor.settings.publicONESIGNAL_REST_KEYinMeteor.settings(i.e. top level settings on server)ONESIGNAL_DISABLE_AUTO_PROMPTinMeteor.settings.public(optional, defaults tofalse- auto prompt is enabled by default)
Usage
To send push notifications, simply call (on the server):
import { OneSignalPushNotification } from 'meteor/matheusccastro:one-signal-push-notification'; await OneSignalPushNotification.sendPushNotification({ title, message, userIds, additionalData, url, notificationId });
To make the notification redirect to some part of your app or to an external page, pass the url property to the additionalData object or as a named parameter to the function.
To enable redirects from push notifications inside your app, do the following (on the client - outside of startup calls):
import { OneSignalPushNotification } from 'meteor/matheusccastro:one-signal-push-notification'; OneSignalPushNotification.setInternalLinkHandler((url) => { ... })
This will register your callback on the one-signal plugin, and it will be called when the received push notification
tries to redirect the user to the page you sent (i.e. additionalData.url exists).
Normally in this handler you would make your router redirect the user to the page.
To prompt the user for push notification permission manually (on the client):
import { OneSignalPushNotification } from 'meteor/matheusccastro:one-signal-push-notification'; const accepted = await OneSignalPushNotification.promptPermission(); // accepted is a boolean indicating whether the user granted permission
Note: This is only useful when ONESIGNAL_DISABLE_AUTO_PROMPT is set to true.
Note: that both the server and client files are lazy imported, so you need to do an import on the architecture you want to use.
Logging
By default, logging will be enabled when Meteor.isDevelopment is true. If you want to change that, call (outside of startup calls):
import { OneSignalPushNotification } from 'meteor/matheusccastro:one-signal-push-notification'; OneSignalPushNotification.enableLog(); OneSignalPushNotification.disableLog();
Metrics
By default, we store the notification that was sent and the audience of it.
The metrics are stored under the OneSignalPushNotificationMeteorMetrics collection. The metric recording is done automatically and if you want, you can
pass a notificationId to your sendPushNotification call, this way you can match a notification to an event that happened on your application.
The schema for the collection is the following:
1{ 2 userId: String, 3 notificationId: String, 4 readAt: Date, 5 createdAt: Date, 6 updatedAt: Date, 7 read: Boolean, 8}
Important
This package stores the playerIds from one-signal in the user document. You may need to adapt your publications to account
for this new field.