react-onesignal
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -43,4 +43,7 @@ module.exports = { | ||
"react/jsx-props-no-spreading": 0, | ||
"prefer-destructuring": 0, | ||
"no-param-reassign": 0, | ||
"import/extensions": 0, | ||
"dot-notation": 0, | ||
}, | ||
}; |
@@ -12,2 +12,9 @@ var DEFAULT_BASE_SCRIPT_ID = 'react-onesignal-base'; | ||
/** | ||
* Gets the window OneSignal instance. | ||
*/ | ||
var getOneSignalInstance = function () { | ||
var OneSignal = window['OneSignal']; | ||
return OneSignal; | ||
}; | ||
/** | ||
* Injects one script into the DOM. | ||
@@ -60,12 +67,20 @@ * @param id script id. | ||
/** | ||
* Array with every possible notification permission state. | ||
*/ | ||
var notificationPermission = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.notificationPermission; }; | ||
/** | ||
* Gets the current notification permission state. | ||
*/ | ||
var getNotificationPermission = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getNotificationPermission(); }; | ||
/** | ||
* Attempt to register for push notifications. | ||
* If the user hasn't authorized push notifications yet, | ||
* this will show a prompt to do so. | ||
*/ | ||
var registerForPushNotifications = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.registerForPushNotifications(); }; | ||
/** | ||
* Sets the email on OneSignal instance. | ||
* @param email email | ||
*/ | ||
var setEmail = function (email) { | ||
var OneSignal = window['OneSignal']; | ||
if (OneSignal) { | ||
return OneSignal === null || OneSignal === void 0 ? void 0 : OneSignal.setEmail(email); | ||
} | ||
return null; | ||
}; | ||
var setEmail = function (email) { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.setEmail(email); }; | ||
/** | ||
@@ -75,10 +90,8 @@ * Gets the email ID configured on OneSignal instance. | ||
*/ | ||
var getEmailId = function () { | ||
var OneSignal = window['OneSignal']; | ||
if (OneSignal) { | ||
return OneSignal === null || OneSignal === void 0 ? void 0 : OneSignal.getEmailId(); | ||
} | ||
return null; | ||
}; | ||
var getEmailId = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getEmailId(); }; | ||
/** | ||
* Returns the Player ID from this browser. | ||
*/ | ||
var getPlayerId = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getUserId(); }; | ||
/** | ||
* Object for manipulating OneSignal. | ||
@@ -88,4 +101,8 @@ */ | ||
initialize: initialize, | ||
notificationPermission: notificationPermission, | ||
getNotificationPermission: getNotificationPermission, | ||
registerForPushNotifications: registerForPushNotifications, | ||
setEmail: setEmail, | ||
getEmailId: getEmailId, | ||
getPlayerId: getPlayerId, | ||
}; | ||
@@ -92,0 +109,0 @@ |
@@ -14,2 +14,9 @@ 'use strict'; | ||
/** | ||
* Gets the window OneSignal instance. | ||
*/ | ||
var getOneSignalInstance = function () { | ||
var OneSignal = window['OneSignal']; | ||
return OneSignal; | ||
}; | ||
/** | ||
* Injects one script into the DOM. | ||
@@ -62,12 +69,20 @@ * @param id script id. | ||
/** | ||
* Array with every possible notification permission state. | ||
*/ | ||
var notificationPermission = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.notificationPermission; }; | ||
/** | ||
* Gets the current notification permission state. | ||
*/ | ||
var getNotificationPermission = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getNotificationPermission(); }; | ||
/** | ||
* Attempt to register for push notifications. | ||
* If the user hasn't authorized push notifications yet, | ||
* this will show a prompt to do so. | ||
*/ | ||
var registerForPushNotifications = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.registerForPushNotifications(); }; | ||
/** | ||
* Sets the email on OneSignal instance. | ||
* @param email email | ||
*/ | ||
var setEmail = function (email) { | ||
var OneSignal = window['OneSignal']; | ||
if (OneSignal) { | ||
return OneSignal === null || OneSignal === void 0 ? void 0 : OneSignal.setEmail(email); | ||
} | ||
return null; | ||
}; | ||
var setEmail = function (email) { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.setEmail(email); }; | ||
/** | ||
@@ -77,10 +92,8 @@ * Gets the email ID configured on OneSignal instance. | ||
*/ | ||
var getEmailId = function () { | ||
var OneSignal = window['OneSignal']; | ||
if (OneSignal) { | ||
return OneSignal === null || OneSignal === void 0 ? void 0 : OneSignal.getEmailId(); | ||
} | ||
return null; | ||
}; | ||
var getEmailId = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getEmailId(); }; | ||
/** | ||
* Returns the Player ID from this browser. | ||
*/ | ||
var getPlayerId = function () { var _a; return (_a = getOneSignalInstance()) === null || _a === void 0 ? void 0 : _a.getUserId(); }; | ||
/** | ||
* Object for manipulating OneSignal. | ||
@@ -90,4 +103,8 @@ */ | ||
initialize: initialize, | ||
notificationPermission: notificationPermission, | ||
getNotificationPermission: getNotificationPermission, | ||
registerForPushNotifications: registerForPushNotifications, | ||
setEmail: setEmail, | ||
getEmailId: getEmailId, | ||
getPlayerId: getPlayerId, | ||
}; | ||
@@ -94,0 +111,0 @@ |
@@ -6,5 +6,9 @@ /** | ||
initialize: (appId: string) => void; | ||
setEmail: (email: string) => Promise<string> | null; | ||
getEmailId: () => Promise<string> | null; | ||
notificationPermission: () => string[]; | ||
getNotificationPermission: () => Promise<string>; | ||
registerForPushNotifications: () => Promise<any>; | ||
setEmail: (email: string) => Promise<string>; | ||
getEmailId: () => Promise<string>; | ||
getPlayerId: () => Promise<string>; | ||
}; | ||
export default ReactOneSignal; |
export interface IOneSignal { | ||
notificationPermission: string[]; | ||
registerForPushNotifications: () => Promise<any>; | ||
getNotificationPermission: () => Promise<string>; | ||
setEmail: (email: string) => Promise<string>; | ||
getEmailId: () => Promise<string>; | ||
getUserId: () => Promise<string>; | ||
} |
{ | ||
"name": "react-onesignal", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "React OneSignal Module: Make it easy to integrate OneSignal with your React App!", | ||
@@ -5,0 +5,0 @@ "author": "pedro-lb", |
@@ -32,7 +32,44 @@ # React OneSignal | ||
``` | ||
```js | ||
import OneSignal from 'react-onesignal'; | ||
OneSignal.initialize('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); | ||
``` | ||
## Advanced Usage | ||
## Player ID | ||
Player ID is an important information on OneSignal. | ||
You can use `getPlayerId` to obtain it. | ||
```js | ||
const playerId = await OneSignal.getPlayerId(); | ||
``` | ||
### Notification Permission Management | ||
To manage notifications: | ||
- Use `notificationPermission` to see all possible states. | ||
- Use `getNotificationPermission` to check current notification permissions. | ||
- Use `registerForPushNotifications` to ask for notification permissions. | ||
```js | ||
// Check all possible permission states | ||
const permissions = OneSignal.notificationPermission; | ||
// Check current permission state | ||
const currentState = await OneSignal.getNotificationPermission(); | ||
// Ask the user for notification permissions, if not granted yet | ||
await OneSignal.registerForPushNotifications(); | ||
``` | ||
### User Email Tracking | ||
You can use `setEmail` and `getEmailId` to track user email. | ||
```js | ||
// Set email to track & notify specific users | ||
@@ -39,0 +76,0 @@ OneSignal.setEmail('my_email@example.com'); |
@@ -1,2 +0,3 @@ | ||
import { IOneSignal } from "./oneSignal.types"; | ||
// eslint-disable-next-line no-unused-vars | ||
import { IOneSignal } from './oneSignal.types'; | ||
@@ -25,2 +26,11 @@ const DEFAULT_BASE_SCRIPT_ID = 'react-onesignal-base'; | ||
/** | ||
* Gets the window OneSignal instance. | ||
*/ | ||
const getOneSignalInstance = () => { | ||
const OneSignal: IOneSignal = window['OneSignal']; | ||
return OneSignal; | ||
}; | ||
/** | ||
* Injects one script into the DOM. | ||
@@ -30,3 +40,6 @@ * @param id script id. | ||
*/ | ||
const injectScript = (id: string, buildScript: (script: HTMLScriptElement) => HTMLScriptElement) => { | ||
const injectScript = ( | ||
id: string, | ||
buildScript: (script: HTMLScriptElement) => HTMLScriptElement, | ||
) => { | ||
const hasScript = !!document.getElementById(id); | ||
@@ -45,3 +58,3 @@ | ||
document.body.appendChild(script); | ||
} | ||
}; | ||
@@ -88,29 +101,35 @@ /** | ||
/** | ||
* Sets the email on OneSignal instance. | ||
* @param email email | ||
* Array with every possible notification permission state. | ||
*/ | ||
const setEmail = (email: string) => { | ||
const OneSignal: IOneSignal = window['OneSignal']; | ||
const notificationPermission = () => getOneSignalInstance()?.notificationPermission; | ||
if (OneSignal) { | ||
return OneSignal?.setEmail(email); | ||
} | ||
/** | ||
* Gets the current notification permission state. | ||
*/ | ||
const getNotificationPermission = () => getOneSignalInstance()?.getNotificationPermission(); | ||
return null; | ||
} | ||
/** | ||
* Attempt to register for push notifications. | ||
* If the user hasn't authorized push notifications yet, | ||
* this will show a prompt to do so. | ||
*/ | ||
const registerForPushNotifications = () => getOneSignalInstance()?.registerForPushNotifications(); | ||
/** | ||
* Sets the email on OneSignal instance. | ||
* @param email email | ||
*/ | ||
const setEmail = (email: string) => getOneSignalInstance()?.setEmail(email); | ||
/** | ||
* Gets the email ID configured on OneSignal instance. | ||
* @param email email | ||
*/ | ||
const getEmailId = () => { | ||
const OneSignal: IOneSignal = window['OneSignal']; | ||
const getEmailId = () => getOneSignalInstance()?.getEmailId(); | ||
if (OneSignal) { | ||
return OneSignal?.getEmailId(); | ||
} | ||
/** | ||
* Returns the Player ID from this browser. | ||
*/ | ||
const getPlayerId = () => getOneSignalInstance()?.getUserId(); | ||
return null; | ||
} | ||
/** | ||
@@ -121,6 +140,10 @@ * Object for manipulating OneSignal. | ||
initialize, | ||
notificationPermission, | ||
getNotificationPermission, | ||
registerForPushNotifications, | ||
setEmail, | ||
getEmailId, | ||
getPlayerId, | ||
}; | ||
export default ReactOneSignal; |
export interface IOneSignal { | ||
notificationPermission: string[], | ||
registerForPushNotifications: () => Promise<any>, | ||
getNotificationPermission: () => Promise<string>, | ||
setEmail: (email: string) => Promise<string>, | ||
getEmailId: () => Promise<string>, | ||
getUserId: () => Promise<string>, | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
28998
446
88