npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

onesignal-vue

v2.0.1

Published

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

Downloads

4,034

Readme

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.

Contents


Vue Compatibility

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

| Vue | OneSignal Plugin | |-----|------------------| | 2 | onesignal-vue | | 3 | onesignal-vue3 |


Install

You can use yarn or npm.

Yarn

yarn add onesignal-vue

npm

npm install --save onesignal-vue

Usage

Plugin setup

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

Code completion

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>

Init Options

You can pass other options to the init function. Use these options to configure personalized prompt options, auto-resubscribe, and more.

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


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' | NotificationClickEvent| |'foregroundWillDisplay'| NotificationForegroundWillDisplayEvent | 'dismiss'| NotificationDismissEvent| |'permissionChange'| boolean| |'permissionPromptDisplay'| void|

Slidedown Namespace

| Event Name | Callback Argument Type | |-|-| |'slidedownShown' | boolean |

Push Subscription Namespace

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


🤝 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!