react-onesignal
Advanced tools
Comparing version 1.1.19 to 1.2.1
@@ -55,7 +55,27 @@ /* eslint-disable no-unused-vars */ | ||
/** | ||
* Take our object of OneSignal events and construct listeners. | ||
* | ||
* @param eventsArr Array of event/callback key/value pairs defined by IOneSignalEvent interface. | ||
* @return string Script snippet for injecting into the native OneSignal.push() method. | ||
*/ | ||
var buildEventListeners = function (eventsArr) { | ||
var returnStr = ''; | ||
// Let's make sure we've got an array that isn't empty. | ||
if (Array.isArray(eventsArr) && eventsArr.length) { | ||
eventsArr.forEach(function (event) { | ||
event.listener = event.listener || 'on'; | ||
returnStr += "OneSignal." + event.listener + "('" + event.event + "', " + event.callback + ");"; | ||
}); | ||
} | ||
return returnStr; | ||
}; | ||
/** | ||
* Provides the module script content to inject. | ||
*/ | ||
var getModuleScriptBody = function (appId, options) { | ||
var getModuleScriptBody = function (appId, options, events) { | ||
if (options === void 0) { options = {}; } | ||
if (events === void 0) { events = []; } | ||
var mappedOptions = mapOptionsObject(options); | ||
return "\n var OneSignal = window.OneSignal || [];\n OneSignal.push(function() {\n OneSignal.init({\n appId: \"" + appId + "\",\n " + mappedOptions + "\n });\n });\n "; | ||
var listeners = buildEventListeners(events); | ||
return "\n var OneSignal = window.OneSignal || [];\n OneSignal.push(function() {\n " + listeners + "\n OneSignal.init({\n appId: \"" + appId + "\",\n " + mappedOptions + "\n });\n });\n "; | ||
}; | ||
@@ -99,5 +119,7 @@ /** | ||
*/ | ||
var injectModuleScript = function (appId, options) { | ||
var injectModuleScript = function (appId, options, events) { | ||
if (options === void 0) { options = {}; } | ||
if (events === void 0) { events = []; } | ||
injectScript(DEFAULT_MODULE_SCRIPT_ID, function (script) { | ||
script.innerHTML = getModuleScriptBody(appId, options); | ||
script.innerHTML = getModuleScriptBody(appId, options, events); | ||
script.async = true; | ||
@@ -110,3 +132,4 @@ return script; | ||
*/ | ||
var initialize = function (appId, options) { | ||
var initialize = function (appId, options, events) { | ||
if (events === void 0) { events = []; } | ||
if (!appId) { | ||
@@ -119,3 +142,3 @@ throw new Error('You need to provide your OneSignal appId.'); | ||
injectBaseScript(); | ||
injectModuleScript(appId, options); | ||
injectModuleScript(appId, options, events); | ||
}; | ||
@@ -122,0 +145,0 @@ /** |
@@ -59,7 +59,27 @@ 'use strict'; | ||
/** | ||
* Take our object of OneSignal events and construct listeners. | ||
* | ||
* @param eventsArr Array of event/callback key/value pairs defined by IOneSignalEvent interface. | ||
* @return string Script snippet for injecting into the native OneSignal.push() method. | ||
*/ | ||
var buildEventListeners = function (eventsArr) { | ||
var returnStr = ''; | ||
// Let's make sure we've got an array that isn't empty. | ||
if (Array.isArray(eventsArr) && eventsArr.length) { | ||
eventsArr.forEach(function (event) { | ||
event.listener = event.listener || 'on'; | ||
returnStr += "OneSignal." + event.listener + "('" + event.event + "', " + event.callback + ");"; | ||
}); | ||
} | ||
return returnStr; | ||
}; | ||
/** | ||
* Provides the module script content to inject. | ||
*/ | ||
var getModuleScriptBody = function (appId, options) { | ||
var getModuleScriptBody = function (appId, options, events) { | ||
if (options === void 0) { options = {}; } | ||
if (events === void 0) { events = []; } | ||
var mappedOptions = mapOptionsObject(options); | ||
return "\n var OneSignal = window.OneSignal || [];\n OneSignal.push(function() {\n OneSignal.init({\n appId: \"" + appId + "\",\n " + mappedOptions + "\n });\n });\n "; | ||
var listeners = buildEventListeners(events); | ||
return "\n var OneSignal = window.OneSignal || [];\n OneSignal.push(function() {\n " + listeners + "\n OneSignal.init({\n appId: \"" + appId + "\",\n " + mappedOptions + "\n });\n });\n "; | ||
}; | ||
@@ -103,5 +123,7 @@ /** | ||
*/ | ||
var injectModuleScript = function (appId, options) { | ||
var injectModuleScript = function (appId, options, events) { | ||
if (options === void 0) { options = {}; } | ||
if (events === void 0) { events = []; } | ||
injectScript(DEFAULT_MODULE_SCRIPT_ID, function (script) { | ||
script.innerHTML = getModuleScriptBody(appId, options); | ||
script.innerHTML = getModuleScriptBody(appId, options, events); | ||
script.async = true; | ||
@@ -114,3 +136,4 @@ return script; | ||
*/ | ||
var initialize = function (appId, options) { | ||
var initialize = function (appId, options, events) { | ||
if (events === void 0) { events = []; } | ||
if (!appId) { | ||
@@ -123,3 +146,3 @@ throw new Error('You need to provide your OneSignal appId.'); | ||
injectBaseScript(); | ||
injectModuleScript(appId, options); | ||
injectModuleScript(appId, options, events); | ||
}; | ||
@@ -126,0 +149,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { OneSignalOptions } from './oneSignal.types'; | ||
import { OneSignalOptions, IOneSignalEvent } from './oneSignal.types'; | ||
/** | ||
@@ -6,3 +6,3 @@ * Object for manipulating OneSignal. | ||
declare const ReactOneSignal: { | ||
initialize: (appId: string, options: OneSignalOptions) => void; | ||
initialize: (appId: string, options: OneSignalOptions, events?: IOneSignalEvent[]) => void; | ||
notificationPermission: () => string[] | null; | ||
@@ -9,0 +9,0 @@ getNotificationPermission: () => Promise<string>; |
@@ -28,1 +28,9 @@ export interface IOneSignal { | ||
} | ||
export interface IOneSignalEventCallback { | ||
callback: (result: any) => any; | ||
} | ||
export interface IOneSignalEvent { | ||
listener?: string; | ||
event: string; | ||
callback: IOneSignalEventCallback; | ||
} |
{ | ||
"name": "react-onesignal", | ||
"version": "1.1.19", | ||
"version": "1.2.1", | ||
"description": "React OneSignal Module: Make it easy to integrate OneSignal with your React App!", | ||
@@ -5,0 +5,0 @@ "author": "pedro-lb", |
@@ -113,2 +113,50 @@ # React OneSignal | ||
## Events and Event Listeners | ||
You can also listen for native OneSignal events like `subscriptionChange`. | ||
To add an event listener to the `OneSignal.push()` array, pass an array of events to the `ReactOneSignal.initialize()` function as the third parameter. | ||
Each object in the array should contain: | ||
* `listener` -- (optional) Default value: `'on'`. | ||
Some events can be listened for via multiple listeners (e.g. `.on()`, `.once()`). | ||
[Check the docs](https://documentation.onesignal.com/docs/web-push-sdk) to see which listeners listen for your event. | ||
Example: `'on'` | `'once'` | ||
* `event` -- Name of the event being listened for. | ||
Example: `'subscriptionChange'` | ||
* `callback` -- Callback function for event. | ||
Example: `(value) => { console.log(value); }` | ||
For documentation on events and event listeners, check out the [Web Push SDK docs](https://documentation.onesignal.com/docs/web-push-sdk). | ||
```js | ||
const events = [ | ||
{ | ||
listener: 'once', | ||
event: 'subscriptionChange', | ||
callback: (isSubscribed) => { | ||
if (true === isSubscribed) { | ||
console.log('The user subscription state is now:', isSubscribed); | ||
} | ||
}, | ||
}, | ||
{ | ||
event: 'notificationDisplay', | ||
callback: (event) => { | ||
console.warn('OneSignal notification displayed:', event); | ||
}, | ||
}, | ||
{ | ||
event: 'notificationDismiss', | ||
callback: (event) => { | ||
console.warn('OneSignal notification dismissed:', event); | ||
}, | ||
}, | ||
]; | ||
ReactOneSignal.initialize(applicationId, options, events); | ||
``` | ||
## Contributing | ||
@@ -118,4 +166,10 @@ | ||
## Made possible by | ||
<a href="https://github.com/pedro-lb/react-onesignal/graphs/contributors"> | ||
<img src="https://contributors-img.web.app/image?repo=pedro-lb/react-onesignal" /> | ||
</a> | ||
## License | ||
MIT © [pedro-lb](https://github.com/pedro-lb) |
// eslint-disable-next-line no-unused-vars | ||
import { IOneSignal, OneSignalOptions } from './oneSignal.types'; | ||
import { IOneSignal, OneSignalOptions, IOneSignalEvent } from './oneSignal.types'; | ||
@@ -65,6 +65,30 @@ const DEFAULT_BASE_SCRIPT_ID = 'react-onesignal-base'; | ||
/** | ||
* Take our object of OneSignal events and construct listeners. | ||
* | ||
* @param eventsArr Array of event/callback key/value pairs defined by IOneSignalEvent interface. | ||
* @return string Script snippet for injecting into the native OneSignal.push() method. | ||
*/ | ||
const buildEventListeners = (eventsArr: IOneSignalEvent[]) => { | ||
let returnStr = ''; | ||
// Let's make sure we've got an array that isn't empty. | ||
if (Array.isArray(eventsArr) && eventsArr.length) { | ||
eventsArr.forEach((event) => { | ||
event.listener = event.listener || 'on'; | ||
returnStr += `OneSignal.${event.listener}('${event.event}', ${event.callback});`; | ||
}); | ||
} | ||
return returnStr; | ||
}; | ||
/** | ||
* Provides the module script content to inject. | ||
*/ | ||
const getModuleScriptBody = (appId: string, options: OneSignalOptions) => { | ||
const getModuleScriptBody = ( | ||
appId: string, | ||
options: OneSignalOptions = {}, | ||
events: IOneSignalEvent[] = [], | ||
) => { | ||
const mappedOptions = mapOptionsObject(options); | ||
const listeners = buildEventListeners(events); | ||
@@ -74,2 +98,3 @@ return ` | ||
OneSignal.push(function() { | ||
${listeners} | ||
OneSignal.init({ | ||
@@ -134,5 +159,9 @@ appId: "${appId}", | ||
*/ | ||
const injectModuleScript = (appId: string, options: OneSignalOptions) => { | ||
const injectModuleScript = ( | ||
appId: string, | ||
options: OneSignalOptions = {}, | ||
events: IOneSignalEvent[] = [], | ||
) => { | ||
injectScript(DEFAULT_MODULE_SCRIPT_ID, (script) => { | ||
script.innerHTML = getModuleScriptBody(appId, options); | ||
script.innerHTML = getModuleScriptBody(appId, options, events); | ||
script.async = true; | ||
@@ -147,3 +176,3 @@ | ||
*/ | ||
const initialize = (appId: string, options: OneSignalOptions) => { | ||
const initialize = (appId: string, options: OneSignalOptions, events: IOneSignalEvent[] = []) => { | ||
if (!appId) { | ||
@@ -158,3 +187,3 @@ throw new Error('You need to provide your OneSignal appId.'); | ||
injectBaseScript(); | ||
injectModuleScript(appId, options); | ||
injectModuleScript(appId, options, events); | ||
}; | ||
@@ -161,0 +190,0 @@ |
@@ -29,1 +29,11 @@ export interface IOneSignal { | ||
} | ||
export interface IOneSignalEventCallback{ | ||
callback: (result: any) => any | ||
} | ||
export interface IOneSignalEvent { | ||
listener?: string; | ||
event: string; | ||
callback: IOneSignalEventCallback | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
69790
1084
174