Skip to content

Commit bbc7b72

Browse files
author
Pooya Parsa
committedNov 17, 2017
feat: add oneSignal module
1 parent f1e1fe1 commit bbc7b72

File tree

9 files changed

+231
-0
lines changed

9 files changed

+231
-0
lines changed
 

‎README.md

+71
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Package | Downloads | Latest | Changelog
2323
@nuxtjs/meta | [![npm](https://img.shields.io/npm/dt/@nuxtjs/meta.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/meta) | [![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/meta/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/meta) | [Changelog](https://github.com/nuxt-community/pwa-module/blob/master/packages/meta/CHANGELOG.md)
2424
@nuxtjs/workbox | [![npm](https://img.shields.io/npm/dt/@nuxtjs/workbox.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/workbox) | [![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/workbox/latest.svg?style=flat-square)](https://npmjs.com/package/@nuxtjs/workbox) | [Changelog](https://github.com/nuxt-community/pwa-module/blob/master/packages/workbox/CHANGELOG.md)
2525
@nuxtjs/icon | [![npm](https://img.shields.io/npm/dt/@nuxtjs/icon.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/icon) | [![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/icon/latest.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/icon) | [Changelog](https://github.com/nuxt-community/pwa-module/blob/master/packages/icon/CHANGELOG.md)
26+
@nuxtjs/onesignal | [![npm](https://img.shields.io/npm/dt/@nuxtjs/onesignal.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/onesignal) | [![npm (scoped with tag)](https://img.shields.io/npm/v/@nuxtjs/onesignal/latest.svg?style=flat-square)](https://www.npmjs.com/package/@nuxtjs/onesignal) | [Changelog](https://github.com/nuxt-community/pwa-module/blob/master/packages/onesignal/CHANGELOG.md)
2627

2728
## Contents
2829

@@ -32,6 +33,7 @@ Package | Downloads | Latest | Changelog
3233
- [Workbox](#workbox)
3334
- [Icon](#icon)
3435
- [Meta](#meta)
36+
- [OneSignal](#onesignal)
3537

3638
## Quick Setup
3739

@@ -157,6 +159,75 @@ Please read this resources if you want to enable `mobileAppIOS` option:
157159
- https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
158160
- https://medium.com/@firt/dont-use-ios-web-app-meta-tag-irresponsibly-in-your-progressive-web-apps-85d70f4438cb
159161

162+
## OneSignal
163+
OneSignal is a Free, high volume and reliable push notification service for websites and mobile applications.[Learn More](https://documentation.onesignal.com/docs/product-overview)
164+
165+
Setting and and using this module is little tricky as OneSignal requires to register it's own Service worker. (see [Web Push SDK Setup (HTTPS)](https://documentation.onesignal.com/docs/web-push-sdk-setup-https))
166+
167+
First add dependency as it is not being installed by default when using PWA module:
168+
169+
```bash
170+
yarn add @nuxtjs/onesignal
171+
# OR
172+
npm i @nuxtjs/onesignal
173+
```
174+
175+
Then add module to `nuxt.config.js` **BEFORE** `@nuxtjs/pwa` and provide options under `oneSignal`:
176+
177+
```js
178+
modules: [
179+
'@nuxtjs/onesignal',
180+
'@nuxtjs/pwa',
181+
],
182+
183+
// Options
184+
oneSignal: {
185+
appId: 'YOUR_APP_ID',
186+
// ...your other init settings
187+
}
188+
```
189+
190+
### Async Functions
191+
This module exposes oneSignal as `$OneSignal` everywhere. So you can call it.
192+
Please note that because of async loading of OneSignal SDK script, every action should be pushed into `$OneSignal` stack.
193+
194+
```js
195+
// Inside page components
196+
this.$OneSignal.push(() => {
197+
this.$OneSignal.isPushNotificationsEnabled((isEnabled) => {
198+
if (isEnabled) {
199+
console.log('Push notifications are enabled!')
200+
} else {
201+
console.log('Push notifications are not enabled yet.')
202+
}
203+
})
204+
})
205+
206+
// Using window and array form
207+
window.$OneSignal.push(['addListenerForNotificationOpened', (data) => {
208+
console.log('Received NotificationOpened:', data }
209+
]);
210+
```
211+
212+
### Change OneSignal SDK Script URL
213+
214+
By default this modules ships with latest SDK dist.
215+
216+
You can use recommended CDN by using `cdn: true` or changing it to a custom value using `OneSignalSDK`.
217+
218+
```js
219+
oneSignal: {
220+
// Use CDN
221+
cdn: true,
222+
223+
// Use any custom URL
224+
OneSignalSDK: 'https://cdn.onesignal.com/sdks/OneSignalSDK.js'
225+
}
226+
```
227+
228+
### References
229+
230+
Please see [Web Push SDK Reference](https://documentation.onesignal.com/docs/web-push-sdk) for all available options and API functions.
160231
161232
## License
162233

‎packages/onesignal/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
👉 Please refer to [nuxt-community/pwa-module](https://github.com/nuxt-community/pwa-module) for documentation.

‎packages/onesignal/dist/OneSignalSDK.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/onesignal/index.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
const path = require('path')
2+
const { writeFileSync, readFileSync } = require('fs')
3+
const hashSum = require('hash-sum')
4+
const debug = require('debug')('nuxt:pwa')
5+
6+
const fixUrl = url => url.replace(/\/\//g, '/').replace(':/', '://')
7+
const isUrl = url => url.indexOf('http') === 0 || url.indexOf('//') === 0
8+
9+
// =============================================
10+
// oneSignal Module
11+
// =============================================
12+
13+
module.exports = function nuxtOneSignal (moduleOptions) {
14+
const hook = () => {
15+
debug('Adding OneSignal')
16+
addOneSignal.call(this, moduleOptions)
17+
}
18+
19+
if (this.options.mode === 'spa') {
20+
return hook()
21+
}
22+
23+
this.nuxt.hook ? this.nuxt.hook('build:before', hook) : this.nuxt.plugin('build', hook)
24+
}
25+
26+
// =============================================
27+
// addOneSignal
28+
// =============================================
29+
30+
function addOneSignal (moduleOptions) {
31+
// Router Base
32+
const routerBase = this.options.router.base
33+
let publicPath = fixUrl(`${routerBase}/${this.options.build.publicPath}`)
34+
if (isUrl(this.options.build.publicPath)) { // CDN
35+
publicPath = this.options.build.publicPath
36+
if (publicPath.indexOf('//') === 0) {
37+
publicPath = '/' + publicPath // Escape fixUrl
38+
}
39+
}
40+
41+
// Merge options
42+
const defaults = {
43+
OneSignalSDK: undefined,
44+
cdn: false,
45+
GcmSenderId: '482941778795',
46+
allowLocalhostAsSecureOrigin: true,
47+
importScripts: [
48+
'/sw.js'
49+
]
50+
}
51+
52+
const options = Object.assign(defaults, moduleOptions, this.options.oneSignal)
53+
54+
if (options.OneSignalSDK === undefined) {
55+
if (options.cdn) {
56+
// Use OneSignalSDK.js from CDN
57+
options.OneSignalSDK = 'https://cdn.onesignal.com/sdks/OneSignalSDK.js'
58+
} else {
59+
// Use OneSignalSDK.js from Dist
60+
const OneSignalSDKJS = readFileSync(path.resolve(__dirname, 'dist/OneSignalSDK.js'))
61+
const OneSignalSDKHash = hashSum(OneSignalSDKJS)
62+
const OneSignalSDKFile = `ons.${OneSignalSDKHash}.js`
63+
64+
options.OneSignalSDK = fixUrl(publicPath + '/' + OneSignalSDKFile)
65+
66+
this.options.build.plugins.push({
67+
apply (compiler) {
68+
compiler.plugin('emit', function (compilation, cb) {
69+
compilation.assets[OneSignalSDKFile] = {
70+
source: () => OneSignalSDKJS,
71+
size: () => OneSignalSDKJS.length
72+
}
73+
cb()
74+
})
75+
}
76+
})
77+
}
78+
}
79+
80+
// Add the oneSignal SDK script to head
81+
this.options.head.script.push({
82+
async: true,
83+
src: options.OneSignalSDK
84+
})
85+
86+
// Adjust manifest for oneSignal
87+
if (!this.options.manifest) {
88+
this.options.manifest = {}
89+
}
90+
if (this.options.manifest.gcm_sender_id) {
91+
debug('WARNING: Overriding gcm_sender_id for OnSignal')
92+
}
93+
this.options.manifest.gcm_sender_id = options.GcmSenderId
94+
95+
// Adjust swURL option of Workbox for oneSignal
96+
if (!this.options.workbox) {
97+
this.options.workbox = {}
98+
}
99+
if (this.options.workbox.swURL) {
100+
debug('WARNING: Overriding swURL for OneSignal')
101+
}
102+
this.options.workbox.swURL = 'OneSignalSDKWorker.js'
103+
104+
// Provide OneSignalSDKWorker.js and OneSignalSDKUpdaterWorker.js
105+
const makeSW = (name, scripts) => {
106+
const workerScript = `importScripts(${scripts.map(i => `'${i}'`).join(', ')})\r\n`
107+
writeFileSync(path.resolve(this.options.srcDir, 'static', name), workerScript, 'utf-8')
108+
}
109+
110+
makeSW('OneSignalSDKWorker.js', [].concat(options.importScripts || []).concat(options.OneSignalSDK))
111+
makeSW('OneSignalSDKUpdaterWorker.js', [options.OneSignalSDK])
112+
113+
// Add OneSignal init plugin
114+
const onsOpts = Object.assign({}, options)
115+
delete onsOpts.OneSignalSDK
116+
delete onsOpts.cdn
117+
delete onsOpts.GcmSenderId
118+
delete onsOpts.importScripts
119+
120+
this.addPlugin({
121+
src: path.resolve(__dirname, 'templates/plugin.js'),
122+
ssr: false,
123+
fileName: 'onesignal.js',
124+
options: {
125+
onsOpts
126+
}
127+
})
128+
}
129+
130+
module.exports.meta = require('./package.json')

‎packages/onesignal/package.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@nuxtjs/onesignal",
3+
"version": "2.0.0",
4+
"license": "MIT",
5+
"main": "index.js",
6+
"repository": "https://github.com/nuxt-community/pwa-module",
7+
"publishConfig": {
8+
"access": "public"
9+
}
10+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var OneSignal = window.OneSignal || [];
2+
3+
OneSignal.push(['init', <%= JSON.stringify(options.onsOpts, null, 2) %>]);
4+
5+
window.$OneSignal = OneSignal
6+
7+
export default function (ctx, inject) {
8+
inject('OneSignal', OneSignal)
9+
}

‎test/fixture/nuxt.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
}
1313
},
1414
modules: [
15+
'@nuxtjs/onesignal',
1516
'@nuxtjs/pwa'
1617
],
1718
manifest: {
@@ -23,5 +24,10 @@ module.exports = {
2324
importScripts: [
2425
'custom-sw.js'
2526
]
27+
},
28+
oneSignal: {
29+
appId: 'd867ac26-f7be-4c62-9fdd-b756a33c4a8f',
30+
OneSignalSDK: 'https://cdn.onesignal.com/sdks/OneSignalSDK.js'
31+
2632
}
2733
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
importScripts('https://cdn.onesignal.com/sdks/OneSignalSDK.js')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
importScripts('/sw.js', 'https://cdn.onesignal.com/sdks/OneSignalSDK.js')

0 commit comments

Comments
 (0)
Please sign in to comment.