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 800k businesses to send 5 billion Push Notifications per day.
You can find more information on OneSignal here.
Upgrading from Version 1? See our migration guide if coming from v1.
Make sure you install a plugin version compatible with your Vue environment.
Vue | OneSignal Plugin |
---|---|
2 | onesignal-vue |
3 | onesignal-vue3 |
You can use yarn
or npm
.
yarn add onesignal-vue
npm install --save onesignal-vue
import Vue from 'vue'
import OneSignalVue from 'onesignal-vue'
Vue.use(OneSignalVue);
Initialize OneSignal with your appId
via the options
parameter:
new Vue({
render: h => h(App),
beforeMount() {
this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
}
}).$mount('#app')
The init
function returns a promise that resolves when OneSignal is loaded.
Examples
await this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
// do other stuff
this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
// do other stuff
});
For code completion to work correctly, make sure you import the plugin (e.g: in child components).
<script>
import OneSignalVue from 'onesignal-vue';
export default {
name: 'HelloWorld',
props: {
msg: String
},
beforeCreate() {
// Example prompting for notification permission
this.$OneSignal.User.PushSubscription.optIn();
}
}
</script>
You can pass other options to the init
function. Use these options to configure personalized prompt options, auto-resubscribe, and more.
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.
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>;
}
See the official OneSignal WebSDK reference for information on all available SDK functions.
Use listeners to react to OneSignal-related events:
Event Name | Callback Argument Type |
---|---|
'click' | NotificationClickEvent |
'foregroundWillDisplay' | NotificationForegroundWillDisplayEvent |
'dismiss' | NotificationDismissEvent |
'permissionChange' | boolean |
'permissionPromptDisplay' | void |
Event Name | Callback Argument Type |
---|---|
'slidedownShown' | boolean |
Event Name | Callback Argument Type |
---|---|
'change' | boolean |
Example
OneSignal.Notifications.addEventListener('click', (event) => {
console.log("The notification was clicked!", event);
});
See the OneSignal WebSDK Reference for all available event listeners.
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Give a βοΈ if this project helped you!
Reach out to us via our Discord server!
Copyright Β© 2022 OneSignal.
This project is Modified MIT licensed.
Enjoy!