Vue JS Setup
The OneSignal Vue Plugin 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.
Requirements
- OneSignal Account
- Your OneSignal App Id, available in Settings > Keys & IDs.
If you have not done so already, you may benefit from following our Web Push Quickstart first.
Vue Compatibility
Make sure you install a plugin version compatible with your Vue environment.
Vue | OneSignal Plugin |
---|---|
2 | onesignal-vue |
3 | onesignal-vue3 |
Install
Yarn
yarn add onesignal-vue
# or
yarn add @onesignal/onesignal-vue3
npm
npm install --save onesignal-vue
# or
npm install --save @onesignal/onesignal-vue3
Usage
Plugin setup
Vue2
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.
//Example 1
await this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' });
// do other stuff
//Example 2
this.$OneSignal.init({ appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }).then(() => {
// do other stuff
});
Vue3
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.
// main
import { createApp } from 'vue'
import OneSignalVuePlugin from '@onesignal/onesignal-vue3'
createApp(App).use(OneSignalVuePlugin, {
appId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}).mount('#app');
or
//main
import { createApp } from 'vue'
import OneSignalVuePlugin from '@onesignal/onesignal-vue3'
createApp(App).use(OneSignalVuePlugin).mount('#app');
// component
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
Code completion
If IntelliSense is not working as expected in your .vue
file, try adding an import from the OneSignal plugin.
<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>
Options
You can pass other options to the init
function. Use these options to configure personalized prompt options, auto-resubscribe, and more.
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 service worker has control of. Recommendation: A non-root path that you will never host pages from. - This prevents conflicts other Service Workers. Example: { scope: "/onesignal" } |
serviceWorkerPath | The path and file to your service worker file with OneSignal included. Example: "/myPath/OneSignalSDKWorker.js" |
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 not placing the file on the 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 as javascript content. (Consider serviceWorkerParam
if you customized it)
OneSignal API
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 OneSignal WebSDK reference for information on all available SDK functions.
Updated about 1 year ago