welcome to onesignal-vue3 👋
This is a JavaScript module that can be used to easily include OneSignal code in a website or app that uses Vue for its front-end codebase.
OneSignal is the world's leader for Mobile Push Notifications, Web Push, and In-App Messaging. It is trusted by 2 million+ businesses to send 9 billion Push Notifications per day.
You can find more information on OneSignal here.
🚧 Version 2 now in Beta!
See our migration guide to get started.
🚧 Version 2 now in Beta!
See our migration guide to get started.
Contents
Vue Compatibility
Make sure you install a plugin version compatible with your Vue environment.
Install
You can use yarn
or npm
.
Yarn
yarn add @onesignal/onesignal-vue3
npm
npm install --save @onesignal/onesignal-vue3
Usage
Plugin setup
In Vue 3, you can pass in the OneSignal initialization options directly as an argument to the use
function. You can still initialize separately if you prefer editor benefits like code-completion.
import { createApp } from 'vue'
import OneSignalVuePlugin from '@onesignal/onesignal-vue3'
createApp(App).use(OneSignalVuePlugin, {
appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}).mount('#app');
or
import { createApp } from 'vue'
import OneSignalVuePlugin from '@onesignal/onesignal-vue3'
createApp(App).use(OneSignalVuePlugin).mount('#app');
this.$OneSignal.init({
appId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
});
The OneSignal plugin automatically exposes a $OneSignal
global property accessible inside the application.
Composition API
You can also leverage Vue's Composition API via the useOneSignal
function that can be called from within setup
.
Reference
Initialization
The init
function returns a promise that resolves when OneSignal is loaded.
Examples
await this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
});
Init Options
You can pass other options to the init
function. Use these options to configure personalized prompt options, auto-resubscribe, and more.
Expand to see more options
Property Name | Type | Description |
---|
appId | string | The ID of your OneSignal app. |
autoRegister | boolean (optional) | Whether or not to automatically register the user. |
autoResubscribe | boolean (optional) | Whether or not to automatically resubscribe the user. |
path | string (optional) | The path to the OneSignal service worker file. |
serviceWorkerPath | string (optional) | The path to the OneSignal service worker script. |
serviceWorkerUpdaterPath | string (optional) | The path to the OneSignal service worker updater script. |
subdomainName | string (optional) | The subdomain of your OneSignal app. |
allowLocalhostAsSecureOrigin | boolean (optional) | Whether or not to allow localhost as a secure origin. |
requiresUserPrivacyConsent | boolean (optional) | Whether or not the user's consent is required. |
persistNotification | boolean (optional) | Whether or not notifications should persist. |
notificationClickHandlerMatch | string (optional) | The URL match pattern for notification clicks. |
notificationClickHandlerAction | string (optional) | The action to perform when a notification is clicked. |
welcomeNotification | object (optional) | The welcome notification configuration. |
notifyButton | object (optional) | The notify button configuration. |
promptOptions | object (optional) | Additional options for the subscription prompt. |
webhooks | object (optional) | The webhook configuration. |
[key: string] | any | Additional properties can be added as needed. |
Service Worker Params
You can customize the location and filenames of service worker assets. You are also able to specify the specific scope that your service worker should control. You can read more here.
In this distribution, you can specify the parameters via the following:
Field | Details |
---|
serviceWorkerParam | Use to specify the scope, or the path the service worker has control of. Example: { scope: "/js/push/onesignal/" } |
serviceWorkerPath | The path to the service worker file. |
Service Worker File
If you haven't done so already, you will need to add the OneSignal Service Worker file to your site (learn more).
The OneSignal SDK file must be publicly accessible. You can put them in your top-level root or a subdirectory. However, if you are placing the file not on top-level root make sure to specify the path via the service worker params in the init options (see section above).
Tip:
Visit https://yoursite.com/OneSignalSDKWorker.js
in the address bar to make sure the files are being served successfully.
Code completion
If IntelliSense is not working as expected in your .vue
file, try adding an import from the OneSignal plugin.
Typescript
This package includes Typescript support.
interface IOneSignalOneSignal {
Slidedown: IOneSignalSlidedown;
Notifications: IOneSignalNotifications;
Session: IOneSignalSession;
User: IOneSignalUser;
Debug: IOneSignalDebug;
login(externalId: string, jwtToken?: string): Promise<void>;
logout(): Promise<void>;
init(options: IInitObject): Promise<void>;
setConsentGiven(consent: boolean): Promise<void>;
setConsentRequired(requiresConsent: boolean): Promise<void>;
}
OneSignal API
See the official OneSignal WebSDK reference for information on all available SDK functions.
Advanced Usage
Events and Event Listeners
Use listeners to react to OneSignal-related events:
Notifications Namespace
Event Name | Callback Argument Type |
---|
'click' | NotificationClickResult |
'foregroundWillDisplay' | NotificationForegroundWillDisplayEvent |
'dismiss' | OSNotificationDataPayload |
'permissionChange' | boolean |
'permissionPromptDisplay' | void |
Expand to see associated types
NotificationClickResult
Property | Description |
---|
actionId | A string representing the action ID associated with the click event |
url | A string representing the URL associated with the click event |
NotificationForegroundWillDisplayEvent
Property | Description |
---|
notification | An OSNotification type object |
OSNotification
Property | Description |
---|
id | Optional string representing the unique identifier of the notification. |
title | Optional string representing the title of the notification. |
body | Optional string representing the body of the notification. |
data | Optional data object associated with the notification. |
url | Optional string representing the URL to be opened when the notification is clicked. |
icon | Optional string representing the URL of the icon to be displayed with the notification. |
image | Optional string representing the URL of the image to be displayed with the notification. |
tag | Optional string representing a unique identifier for a group of notifications. |
requireInteraction | Optional boolean indicating whether the notification requires user interaction or not. |
renotify | Optional boolean indicating whether the notification should be replaced or not, if a notification with the same tag is already displayed. |
actions | Optional array of NotificationActionButton objects representing the action buttons associated with the notification. |
NotificationActionButton
Property | Description |
---|
action | A string representing the action associated with the button. |
title | A string representing the title of the button. |
icon | Optional string representing the URL of the icon to be displayed with the button. |
url | Optional string representing the URL to be opened when the button is clicked. |
OSNotificationDataPayload
Property | Description |
---|
id | A string representing the unique identifier of the notification data payload. |
content | A string representing the content of the notification data payload. |
heading | Optional string representing the heading of the notification data payload. |
url | Optional string representing the URL to be opened when the notification data payload is clicked. |
data | Optional object containing additional data associated with the notification data payload. |
rr | Optional string with value 'y' or 'n' representing whether or not the notification has Confirmed Delivery. |
icon | Optional string representing the URL of the icon to be displayed with the notification data payload. |
image | Optional string representing the URL of the image to be displayed with the notification data payload. |
tag | Optional string representing a unique identifier for a group of notification data payloads. |
badge | Optional string representing the URL of the badge to be displayed with the notification data payload. |
vibrate | Optional array of numbers representing the vibration pattern of the notification data payload. |
buttons | Optional array of NotificationButtonData objects representing the button data associated with the notification data payload. |
NotificationButtonData
Property | Description |
---|
url | A string representing the URL to be opened when the button is clicked. |
id | A string representing the ID of the action. |
action | A string representing the type of the action (inherited from NotificationAction ). |
title | A string representing the title of the action button (inherited from NotificationAction ). |
icon | Optional string representing the URL of the icon to be displayed with the action button. |
Slidedown Namespace
Event Name | Callback Argument Type |
---|
'slidedownShown' | boolean |
Push Subscription Namespace
Event Name | Callback Argument Type |
---|
'change' | boolean |
Example
this.$OneSignal.Notifications.addEventListener('change', (result) => {
console.log("The notification was clicked!", result);
});
See the OneSignal WebSDK Reference for all available event listeners.
🤝 Contributing
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
Give a ⭐️ if this project helped you!
OneSignal
Discord
Reach out to us via our Discord server!
📝 License
Copyright © 2023 OneSignal.
This project is Modified MIT licensed.
Enjoy!