Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@onesignal/onesignal-vue3

Package Overview
Dependencies
Maintainers
6
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@onesignal/onesignal-vue3

Vue 3 OneSignal Plugin: Make it easy to integrate OneSignal with your Vue App!

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.5K
decreased by-1.16%
Maintainers
6
Weekly downloads
 
Created
Source

welcome to onesignal-vue3 👋

npm version npm downloads

Vue OneSignal Plugin: Make it easy to integrate OneSignal with your Vue App!

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.

  • 🏠 Homepage
  • 🖤 npm

Contents


Vue Compatibility

Make sure you install a plugin version compatible with your Vue environment.

VueOneSignal Plugin
2onesignal-vue
3onesignal-vue3

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.

// 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

Initialization

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
});

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:

FieldDetails
serviceWorkerParamUse to specify the scope, or the path the service worker has control of. Example: { scope: "/js/push/onesignal/" }
serviceWorkerPathThe 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.

intellisense

Typescript

This package includes Typescript support.

interface IOneSignal {
  init(options: IInitObject): Promise<void>;
  on(event: string, listener: () => void): void;
  off(event: string, listener: () => void): void;
  once(event: string, listener: () => void): void;
  isPushNotificationsEnabled(callback?: Action<boolean>): Promise<boolean>;
  showHttpPrompt(options?: AutoPromptOptions): Promise<void>;
  registerForPushNotifications(options?: RegisterOptions): Promise<void>;
  setDefaultNotificationUrl(url: string): Promise<void>;
  setDefaultTitle(title: string): Promise<void>;
  getTags(callback?: Action<any>): Promise<void>;
  sendTag(key: string, value: any, callback?: Action<Object>): Promise<Object | null>;
  sendTags(tags: TagsObject<any>, callback?: Action<Object>): Promise<Object | null>;
  deleteTag(tag: string): Promise<Array<string>>;
  deleteTags(tags: Array<string>, callback?: Action<Array<string>>): Promise<Array<string>>;
  addListenerForNotificationOpened(callback?: Action<Notification>): Promise<void>;
  setSubscription(newSubscription: boolean): Promise<void>;
  showHttpPermissionRequest(options?: AutoPromptOptions): Promise<any>;
  showNativePrompt(): Promise<void>;
  showSlidedownPrompt(options?: AutoPromptOptions): Promise<void>;
  showCategorySlidedown(options?: AutoPromptOptions): Promise<void>;
  showSmsSlidedown(options?: AutoPromptOptions): Promise<void>;
  showEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
  showSmsAndEmailSlidedown(options?: AutoPromptOptions): Promise<void>;
  getNotificationPermission(onComplete?: Function): Promise<NotificationPermission>;
  getUserId(callback?: Action<string | undefined | null>): Promise<string | undefined | null>;
  getSubscription(callback?: Action<boolean>): Promise<boolean>;
  setEmail(email: string, options?: SetEmailOptions): Promise<string | null>;
  setSMSNumber(smsNumber: string, options?: SetSMSOptions): Promise<string | null>;
  logoutEmail(): Promise<void>;
  logoutSMS(): Promise<void>;
  setExternalUserId(externalUserId: string | undefined | null, authHash?: string): Promise<void>;
  removeExternalUserId(): Promise<void>;
  getExternalUserId(): Promise<string | undefined | null>;
  provideUserConsent(consent: boolean): Promise<void>;
  getEmailId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
  getSMSId(callback?: Action<string | undefined>): Promise<string | null | undefined>;
  sendOutcome(outcomeName: string, outcomeWeight?: number | undefined): 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:

  • subscriptionChange
  • permissionPromptDisplay
  • notificationPermissionChange
  • popoverShown
  • customPromptClick
  • notificationDisplay
  • notificationDismiss

Example

this.$OneSignal.on('subscriptionChange', function(isSubscribed) {
  console.log("The user's subscription state is now:", isSubscribed);
});

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 © 2022 OneSignal.
This project is Modified MIT licensed.

Enjoy!

Keywords

FAQs

Package last updated on 08 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc