Permalink
...
Checking mergeability…
Don’t worry, you can still create the pull request.
Comparing changes
Open a pull request
- 1 commit
- 6 files changed
- 0 commit comments
- 1 contributor
Unified
Split
Showing
with
565 additions
and 230 deletions.
- +5 −0 .babelrc
- +1 −1 .editorconfig
- +17 −0 .eslintrc
- +55 −43 package.json
- +264 −0 src/js/push.js
- +223 −186 www/push.js
View
5
.babelrc
| @@ -0,0 +1,5 @@ | ||
| +{ | ||
| + "presets": [ | ||
| + "es2015" | ||
| + ] | ||
| +} |
View
2
.editorconfig
| @@ -6,7 +6,7 @@ root = true | ||
| [*] | ||
| charset = utf-8 | ||
| end_of_line = lf | ||
| -indent_size = 4 | ||
| +indent_size = 2 | ||
| indent_style = space | ||
| insert_final_newline = true | ||
| trim_trailing_whitespace = true | ||
View
17
.eslintrc
| @@ -0,0 +1,17 @@ | ||
| +{ | ||
| + "extends": "airbnb", | ||
| + "parser": "babel-eslint", | ||
| + "ecmaFeatures": { | ||
| + "experimentalObjectRestSpread": true | ||
| + }, | ||
| + "rules": { | ||
| + "spaced-comment": 0, | ||
| + "no-console": 0, | ||
| + "no-unused-expressions": [2, { "allowShortCircuit": true }] | ||
| + }, | ||
| + "env": { | ||
| + "node": true, | ||
| + "mocha": true, | ||
| + "browser": true | ||
| + } | ||
| +} |
View
98
package.json
| @@ -1,46 +1,58 @@ | ||
| { | ||
| - "name": "phonegap-plugin-push", | ||
| - "description": "Register and receive push notifications.", | ||
| - "version": "1.7.1", | ||
| - "homepage": "http://github.com/phonegap/phonegap-plugin-push#readme", | ||
| - "repository": { | ||
| - "type": "git", | ||
| - "url": "git://github.com/phonegap/phonegap-plugin-push.git" | ||
| - }, | ||
| - "bugs": { | ||
| - "url": "https://github.com/phonegap/phonegap-plugin-push/issues" | ||
| - }, | ||
| - "cordova": { | ||
| - "id": "phonegap-plugin-push", | ||
| - "platforms": [ | ||
| - "ios", | ||
| - "android", | ||
| - "windows", | ||
| - "browser" | ||
| - ] | ||
| - }, | ||
| - "keywords": [ | ||
| - "ecosystem:cordova", | ||
| - "ecosystem:phonegap", | ||
| - "cordova-ios", | ||
| - "cordova-android", | ||
| - "cordova-windows8", | ||
| - "cordova-windows", | ||
| - "cordova-wp8", | ||
| - "cordova-browser" | ||
| - ], | ||
| - "engines": [ | ||
| - { | ||
| - "name": "cordova", | ||
| - "version": ">=3.0.0" | ||
| - } | ||
| - ], | ||
| - "author": "Adobe PhoneGap Team", | ||
| - "license": "APL", | ||
| - "scripts": { | ||
| - "test": "jasmine-node --color spec" | ||
| - }, | ||
| - "devDependencies": { | ||
| - "jasmine-node": "1.14.5" | ||
| + "name": "phonegap-plugin-push", | ||
| + "description": "Register and receive push notifications.", | ||
| + "version": "1.7.1", | ||
| + "homepage": "http://github.com/phonegap/phonegap-plugin-push#readme", | ||
| + "repository": { | ||
| + "type": "git", | ||
| + "url": "git://github.com/phonegap/phonegap-plugin-push.git" | ||
| + }, | ||
| + "bugs": { | ||
| + "url": "https://github.com/phonegap/phonegap-plugin-push/issues" | ||
| + }, | ||
| + "cordova": { | ||
| + "id": "phonegap-plugin-push", | ||
| + "platforms": [ | ||
| + "ios", | ||
| + "android", | ||
| + "windows", | ||
| + "browser" | ||
| + ] | ||
| + }, | ||
| + "keywords": [ | ||
| + "ecosystem:cordova", | ||
| + "ecosystem:phonegap", | ||
| + "cordova-ios", | ||
| + "cordova-android", | ||
| + "cordova-windows8", | ||
| + "cordova-windows", | ||
| + "cordova-wp8", | ||
| + "cordova-browser" | ||
| + ], | ||
| + "engines": [ | ||
| + { | ||
| + "name": "cordova", | ||
| + "version": ">=3.0.0" | ||
| } | ||
| + ], | ||
| + "author": "Adobe PhoneGap Team", | ||
| + "license": "APL", | ||
| + "scripts": { | ||
| + "build": "babel src/js --out-dir www", | ||
| + "build:watch": "nodemon -w ./src/js -e js -x npm run build", | ||
| + "test": "jasmine-node --color spec" | ||
| + }, | ||
| + "devDependencies": { | ||
| + "babel-cli": "^6.10.1", | ||
| + "babel-core": "^6.10.4", | ||
| + "babel-eslint": "^6.1.0", | ||
| + "babel-preset-es2015": "^6.9.0", | ||
| + "eslint": "^2.13.1", | ||
| + "eslint-config-airbnb": "^9.0.1", | ||
| + "eslint-plugin-import": "^1.9.2", | ||
| + "eslint-plugin-jsx-a11y": "^1.5.3", | ||
| + "eslint-plugin-react": "^5.2.2", | ||
| + "jasmine-node": "1.14.5", | ||
| + "nodemon": "^1.9.2" | ||
| + } | ||
| } |
View
264
src/js/push.js
| @@ -0,0 +1,264 @@ | ||
| +/* global cordova:false */ | ||
| +/* globals window */ | ||
| + | ||
| +/*! | ||
| + * Module dependencies. | ||
| + */ | ||
| + | ||
| +const exec = cordova.require('cordova/exec'); | ||
| + | ||
| +class PushNotification { | ||
| + /** | ||
| + * PushNotification constructor. | ||
| + * | ||
| + * @param {Object} options to initiate Push Notifications. | ||
| + * @return {PushNotification} instance that can be monitored and cancelled. | ||
| + */ | ||
| + constructor(options) { | ||
| + this.handlers = { | ||
| + registration: [], | ||
| + notification: [], | ||
| + error: [], | ||
| + }; | ||
| + | ||
| + // require options parameter | ||
| + if (typeof options === 'undefined') { | ||
| + throw new Error('The options argument is required.'); | ||
| + } | ||
| + | ||
| + // store the options to this object instance | ||
| + this.options = options; | ||
| + | ||
| + // triggered on registration and notification | ||
| + const success = (result) => { | ||
| + if (result && typeof result.registrationId !== 'undefined') { | ||
| + this.emit('registration', result); | ||
| + } else if (result && result.additionalData && | ||
| + typeof result.additionalData.callback !== 'undefined') { | ||
| + const executeFunctionByName = (functionName, context, ...args) => { | ||
| + const namespaces = functionName.split('.'); | ||
| + const func = namespaces.pop(); | ||
| + for (let i = 0; i < namespaces.length; i++) { | ||
| + context = context[namespaces[i]]; | ||
| + } | ||
| + return context[func].apply(context, args); | ||
| + }; | ||
| + | ||
| + executeFunctionByName(result.additionalData.callback, window, result); | ||
| + } else if (result) { | ||
| + this.emit('notification', result); | ||
| + } | ||
| + }; | ||
| + | ||
| + // triggered on error | ||
| + const fail = (msg) => { | ||
| + const e = (typeof msg === 'string') ? new Error(msg) : msg; | ||
| + this.emit('error', e); | ||
| + }; | ||
| + | ||
| + // wait at least one process tick to allow event subscriptions | ||
| + setTimeout(() => { | ||
| + exec(success, fail, 'PushNotification', 'init', [options]); | ||
| + }, 10); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Unregister from push notifications | ||
| + */ | ||
| + unregister(successCallback, errorCallback = () => {}, options) { | ||
| + if (typeof errorCallback !== 'function') { | ||
| + console.log('PushNotification.unregister failure: failure parameter not a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + if (typeof successCallback !== 'function') { | ||
| + console.log('PushNotification.unregister failure: success callback parameter ' + | ||
| + ' must be a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + const cleanHandlersAndPassThrough = () => { | ||
| + if (!options) { | ||
| + this.handlers = { | ||
| + registration: [], | ||
| + notification: [], | ||
| + error: [], | ||
| + }; | ||
| + } | ||
| + successCallback(); | ||
| + }; | ||
| + | ||
| + exec(cleanHandlersAndPassThrough, errorCallback, 'PushNotification', 'unregister', [options]); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Call this to set the application icon badge | ||
| + */ | ||
| + setApplicationIconBadgeNumber(successCallback, errorCallback = () => {}, badge) { | ||
| + if (typeof errorCallback !== 'function') { | ||
| + console.log('PushNotification.setApplicationIconBadgeNumber failure: failure ' + | ||
| + 'parameter not a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + if (typeof successCallback !== 'function') { | ||
| + console.log('PushNotification.setApplicationIconBadgeNumber failure: success ' + | ||
| + 'callback parameter must be a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + exec(successCallback, errorCallback, 'PushNotification', | ||
| + 'setApplicationIconBadgeNumber', [{ badge }]); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Get the application icon badge | ||
| + */ | ||
| + | ||
| + getApplicationIconBadgeNumber(successCallback, errorCallback = () => {}) { | ||
| + if (typeof errorCallback !== 'function') { | ||
| + console.log('PushNotification.getApplicationIconBadgeNumber failure: failure ' + | ||
| + 'parameter not a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + if (typeof successCallback !== 'function') { | ||
| + console.log('PushNotification.getApplicationIconBadgeNumber failure: success ' + | ||
| + 'callback parameter must be a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + exec(successCallback, errorCallback, 'PushNotification', 'getApplicationIconBadgeNumber', []); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Clear all notifications | ||
| + */ | ||
| + | ||
| + clearAllNotifications(successCallback, errorCallback = () => {}) { | ||
| + if (typeof errorCallback !== 'function') { | ||
| + console.log('PushNotification.clearAllNotifications failure: failure parameter ' + | ||
| + 'not a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + if (typeof successCallback !== 'function') { | ||
| + console.log('PushNotification.clearAllNotifications failure: success callback ' + | ||
| + 'parameter must be a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + exec(successCallback, errorCallback, 'PushNotification', 'clearAllNotifications', []); | ||
| + } | ||
| + /** | ||
| + * Listen for an event. | ||
| + * | ||
| + * The following events are supported: | ||
| + * | ||
| + * - registration | ||
| + * - notification | ||
| + * - error | ||
| + * | ||
| + * @param {String} eventName to subscribe to. | ||
| + * @param {Function} callback triggered on the event. | ||
| + */ | ||
| + | ||
| + on(eventName, callback) { | ||
| + if (this.handlers.hasOwnProperty(eventName)) { | ||
| + this.handlers[eventName].push(callback); | ||
| + } | ||
| + } | ||
| + | ||
| + /** | ||
| + * Remove event listener. | ||
| + * | ||
| + * @param {String} eventName to match subscription. | ||
| + * @param {Function} handle function associated with event. | ||
| + */ | ||
| + | ||
| + off(eventName, handle) { | ||
| + if (this.handlers.hasOwnProperty(eventName)) { | ||
| + const handleIndex = this.handlers[eventName].indexOf(handle); | ||
| + if (handleIndex >= 0) { | ||
| + this.handlers[eventName].splice(handleIndex, 1); | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + /** | ||
| + * Emit an event. | ||
| + * | ||
| + * This is intended for internal use only. | ||
| + * | ||
| + * @param {String} eventName is the event to trigger. | ||
| + * @param {*} all arguments are passed to the event listeners. | ||
| + * | ||
| + * @return {Boolean} is true when the event is triggered otherwise false. | ||
| + */ | ||
| + | ||
| + emit(...args) { | ||
| + const eventName = args.shift(); | ||
| + | ||
| + if (!this.handlers.hasOwnProperty(eventName)) { | ||
| + return false; | ||
| + } | ||
| + | ||
| + for (let i = 0, length = this.handlers[eventName].length; i < length; i++) { | ||
| + const callback = this.handlers[eventName][i]; | ||
| + if (typeof callback === 'function') { | ||
| + callback.apply(undefined, args); | ||
| + } else { | ||
| + console.log(`event handler: ${eventName} must be a function`); | ||
| + } | ||
| + } | ||
| + | ||
| + return true; | ||
| + } | ||
| + | ||
| + finish(successCallback = () => {}, errorCallback = () => {}, id = 'handler') { | ||
| + if (typeof successCallback !== 'function') { | ||
| + console.log('finish failure: success callback parameter must be a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + if (typeof errorCallback !== 'function') { | ||
| + console.log('finish failure: failure parameter not a function'); | ||
| + return; | ||
| + } | ||
| + | ||
| + exec(successCallback, errorCallback, 'PushNotification', 'finish', [id]); | ||
| + } | ||
| +} | ||
| + | ||
| +/*! | ||
| + * Push Notification Plugin. | ||
| + */ | ||
| + | ||
| +module.exports = { | ||
| + /** | ||
| + * Register for Push Notifications. | ||
| + * | ||
| + * This method will instantiate a new copy of the PushNotification object | ||
| + * and start the registration process. | ||
| + * | ||
| + * @param {Object} options | ||
| + * @return {PushNotification} instance | ||
| + */ | ||
| + | ||
| + init: (options) => { | ||
| + return new PushNotification(options); | ||
| + }, | ||
| + | ||
| + hasPermission: (successCallback, errorCallback) => { | ||
| + exec(successCallback, errorCallback, 'PushNotification', 'hasPermission', []); | ||
| + }, | ||
| + | ||
| + /** | ||
| + * PushNotification Object. | ||
| + * | ||
| + * Expose the PushNotification object for direct use | ||
| + * and testing. Typically, you should use the | ||
| + * .init helper method. | ||
| + */ | ||
| + PushNotification, | ||
| +}; |
Oops, something went wrong.