2 Commits 5e7cf35acc ... fa341173e9

Author SHA1 Message Date
  halphass fa341173e9 @inizializzazione push plugin 7 years ago
  halphass 28a788e219 @inizializzazione push plugin 7 years ago
5 changed files with 398 additions and 31 deletions
  1. 2 0
      .gitignore
  2. 28 30
      config.xml
  3. 1 0
      www/index.html
  4. 41 1
      www/js/main.js
  5. 326 0
      www/js/vendor/push.js

+ 2 - 0
.gitignore

@@ -38,4 +38,6 @@ platforms/*
 .Spotlight-V100
 .Trashes
 ehthumbs.db
+/node_modules
 Thumbs.db
+.idea

+ 28 - 30
config.xml

@@ -1,35 +1,33 @@
 <?xml version='1.0' encoding='utf-8'?>
-    <widget id="xm24.digitigrafo.it" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
-        <name>I XM24</name>
-        <description>
+<widget id="xm24.digitigrafo.it" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
+    <name>I XM24</name>
+    <description>
             XM24
         </description>
-        <author email="info@digitigrafo.it" href="http://hacklabbo.indivia.net">
+    <author email="info@digitigrafo.it" href="http://hacklabbo.indivia.net">
             hacklabbo.indivia.net - digitigrafo.it
         </author>
-        <content src="index.html" />
-        <plugin name="cordova-plugin-whitelist" spec="1" />
-        <access origin="*" />
-        <!-- DA TAPPARE!!
-        <access origin="127.0.0.1" />
-        <access origin="//www.ecn.org/xm24/*" /-->
-        <allow-intent href="http://*/*" />
-        <allow-intent href="https://*/*" />
-        <allow-intent href="tel:*" />
-        <allow-intent href="sms:*" />
-        <allow-intent href="mailto:*" />
-        <allow-intent href="geo:*" />
-        <platform name="android">
-            <allow-intent href="market:*" />
-        </platform>
-        <platform name="ios">
-            <allow-intent href="itms:*" />
-            <allow-intent href="itms-apps:*" />
-        </platform>
-
-
-
-        <platform name="android">
-            <icon src="res/icon.png" />
-        </platform>
-    </widget>
+    <content src="index.html" />
+    <plugin name="cordova-plugin-whitelist" spec="1" />
+    <access origin="*" />
+    <allow-intent href="http://*/*" />
+    <allow-intent href="https://*/*" />
+    <allow-intent href="tel:*" />
+    <allow-intent href="sms:*" />
+    <allow-intent href="mailto:*" />
+    <allow-intent href="geo:*" />
+    <platform name="android">
+        <allow-intent href="market:*" />
+    </platform>
+    <platform name="ios">
+        <allow-intent href="itms:*" />
+        <allow-intent href="itms-apps:*" />
+    </platform>
+    <platform name="android">
+        <icon src="res/icon.png" />
+    </platform>
+    <plugin name="phonegap-plugin-push" spec="~1.9.4">
+        <variable name="SENDER_ID" value="667898382143" />
+    </plugin>
+    <plugin name="cordova-sqlite-storage" spec="~2.0.2" />
+</widget>

+ 1 - 0
www/index.html

@@ -35,6 +35,7 @@
     <script src="js/main.js"></script>
     <script src="js/vendor/jquery.mobile-1.4.5.min.js"></script>
     <script type="text/javascript" src="cordova.js"></script>
+    <script src="js/vendor/push.js"></script>
     <script type="text/javascript">
         $(document).on("mobileinit", function () {
 

+ 41 - 1
www/js/main.js

@@ -827,4 +827,44 @@ function checkAndReset() {
         alert('ERR_RESET');
         return false;
     }
-};
+};
+
+function onDeviceReadyPush() {
+    var push = PushNotification.init({
+        android: {
+            senderID: "667898382143"
+        },
+        browser: {
+            pushServiceURL: 'http://push.api.phonegap.com/v1/push'
+        },
+        ios: {
+            alert: "true",
+            badge: "true",
+            sound: "true"
+        },
+        windows: {}
+    });
+
+    push.on('registration', function(registration) {
+        // console.log(JSON.stringify(registration));
+        var url = "https://xm.bus.pm/api/d/c?device=" + registration.registrationId;
+        $.get(url, function( data ) {
+            console.log(JSON.stringify(data));
+            console.log(JSON.stringify(data));
+            // $(".hiddendiv").append('<li>REQ A2A: '+ data +'</li>');
+        });
+    });
+
+    push.on('notification', function(data) {
+        console.log(JSON.stringify(data));
+        console.log(JSON.stringify(data));
+    });
+
+    push.on('error', function(e) {
+        console.log(JSON.stringify(e));
+        console.log(JSON.stringify(e));
+    });
+};
+
+
+document.addEventListener('deviceready', onDeviceReadyPush, true);

+ 326 - 0
www/js/vendor/push.js

@@ -0,0 +1,326 @@
+/* global cordova:false */
+/* globals window */
+
+/*!
+ * Module dependencies.
+ */
+
+var exec = cordova.require('cordova/exec');
+
+/**
+ * PushNotification constructor.
+ *
+ * @param {Object} options to initiate Push Notifications.
+ * @return {PushNotification} instance that can be monitored and cancelled.
+ */
+
+var PushNotification = function(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
+    var that = this;
+    var success = function(result) {
+        if (result && typeof result.registrationId !== 'undefined') {
+            that.emit('registration', result);
+        } else if (result && result.additionalData && typeof result.additionalData.actionCallback !== 'undefined') {
+            var executeFuctionOrEmitEventByName = function(callbackName, context, arg) {
+              var namespaces = callbackName.split('.');
+              var func = namespaces.pop();
+              for (var i = 0; i < namespaces.length; i++) {
+                context = context[namespaces[i]];
+              }
+
+              if (typeof context[func] === 'function') {
+                context[func].call(context, arg);
+              } else {
+                that.emit(callbackName, arg);
+              }
+            };
+
+            executeFuctionOrEmitEventByName(result.additionalData.actionCallback, window, result);
+        } else if (result) {
+            that.emit('notification', result);
+        }
+    };
+
+    // triggered on error
+    var fail = function(msg) {
+        var e = (typeof msg === 'string') ? new Error(msg) : msg;
+        that.emit('error', e);
+    };
+
+    // wait at least one process tick to allow event subscriptions
+    setTimeout(function() {
+        exec(success, fail, 'PushNotification', 'init', [options]);
+    }, 10);
+};
+
+/**
+ * Unregister from push notifications
+ */
+
+PushNotification.prototype.unregister = function(successCallback, errorCallback, options) {
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    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;
+    }
+
+    var that = this;
+    var cleanHandlersAndPassThrough = function() {
+        if (!options) {
+            that._handlers = {
+                'registration': [],
+                'notification': [],
+                'error': []
+            };
+        }
+        successCallback();
+    };
+
+    exec(cleanHandlersAndPassThrough, errorCallback, 'PushNotification', 'unregister', [options]);
+};
+
+/**
+ * subscribe to a topic
+ * @param   {String}      topic               topic to subscribe
+ * @param   {Function}    successCallback     success callback
+ * @param   {Function}    errorCallback       error callback
+ * @return  {void}
+ */
+PushNotification.prototype.subscribe = function(topic, successCallback, errorCallback) {
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    if (typeof errorCallback !== 'function')  {
+        console.log('PushNotification.subscribe failure: failure parameter not a function');
+        return;
+    }
+
+    if (typeof successCallback !== 'function') {
+        console.log('PushNotification.subscribe failure: success callback parameter must be a function');
+        return;
+    }
+
+    exec(successCallback, errorCallback, 'PushNotification', 'subscribe', [topic]);
+};
+
+/**
+ * unsubscribe to a topic
+ * @param   {String}      topic               topic to unsubscribe
+ * @param   {Function}    successCallback     success callback
+ * @param   {Function}    errorCallback       error callback
+ * @return  {void}
+ */
+PushNotification.prototype.unsubscribe = function(topic, successCallback, errorCallback) {
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    if (typeof errorCallback !== 'function')  {
+        console.log('PushNotification.unsubscribe failure: failure parameter not a function');
+        return;
+    }
+
+    if (typeof successCallback !== 'function') {
+        console.log('PushNotification.unsubscribe failure: success callback parameter must be a function');
+        return;
+    }
+
+    exec(successCallback, errorCallback, 'PushNotification', 'unsubscribe', [topic]);
+};
+
+/**
+ * Call this to set the application icon badge
+ */
+
+PushNotification.prototype.setApplicationIconBadgeNumber = function(successCallback, errorCallback, badge) {
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    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: badge}]);
+};
+
+/**
+ * Get the application icon badge
+ */
+
+PushNotification.prototype.getApplicationIconBadgeNumber = function(successCallback, errorCallback) {
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    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', []);
+};
+
+/**
+ * Get the application icon badge
+ */
+
+PushNotification.prototype.clearAllNotifications = function(successCallback, errorCallback) {
+    if (!successCallback) { successCallback = function() {}; }
+    if (!errorCallback) { errorCallback = function() {}; }
+
+    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.
+ *
+ * Any event is supported, but the following are built-in:
+ *
+ *   - registration
+ *   - notification
+ *   - error
+ *
+ * @param {String} eventName to subscribe to.
+ * @param {Function} callback triggered on the event.
+ */
+
+PushNotification.prototype.on = function(eventName, callback) {
+    if (!this._handlers.hasOwnProperty(eventName)) {
+        this._handlers[eventName] = [];
+    }
+    this._handlers[eventName].push(callback);
+};
+
+/**
+ * Remove event listener.
+ *
+ * @param {String} eventName to match subscription.
+ * @param {Function} handle function associated with event.
+ */
+
+PushNotification.prototype.off = function (eventName, handle) {
+    if (this._handlers.hasOwnProperty(eventName)) {
+        var 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.
+ */
+
+PushNotification.prototype.emit = function() {
+    var args = Array.prototype.slice.call(arguments);
+    var eventName = args.shift();
+
+    if (!this._handlers.hasOwnProperty(eventName)) {
+        return false;
+    }
+
+    for (var i = 0, length = this._handlers[eventName].length; i < length; i++) {
+        var 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;
+};
+
+PushNotification.prototype.finish = function(successCallback, errorCallback, id) {
+    if (!successCallback) { successCallback = function() {}; }
+    if (!errorCallback) { errorCallback = function() {}; }
+    if (!id) { 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.
+ */
+var module = {};
+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: function(options) {
+        return new PushNotification(options);
+    },
+
+    hasPermission: function(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: PushNotification
+};