JAVASCRIPT   48

appp push init

Guest on 21st May 2022 02:19:47 AM

  1. window.appp_push = (function(window, document, undefined){
  2.     'use strict';
  3.  
  4.     var push = {};
  5.  
  6.     /**
  7.      * Application Constructor
  8.      * @since  1.0.0
  9.      */
  10.     push.initialize = function() {
  11.         push.bindEvents();
  12.     };
  13.  
  14.     /**
  15.      * Bind Event Listeners
  16.      *
  17.      * Bind any events that are required on startup. Common events are:
  18.      * 'load', 'deviceready', 'offline', and 'online'.
  19.      *
  20.      * @since  1.0.0
  21.      * @return {[type]} [description]
  22.      */
  23.     push.bindEvents = function() {
  24.         document.addEventListener( 'deviceready', push.onDeviceReady, false );
  25.     };
  26.  
  27.     /**
  28.      * deviceready Event Handler
  29.      * @since  1.0.0
  30.      */
  31.     push.onDeviceReady = function() {
  32.         // check and add device id
  33.         var device_id = PushWoosh.getHWId();
  34.         push.receivedEvent('deviceready');
  35.        
  36.         var url = apppCore.ajaxurl;
  37.        
  38.         if ( ! url )
  39.           return;
  40.  
  41.         // alert(url);
  42.        
  43.         jQuery.ajax({
  44.           type: 'POST',
  45.           dataType: "json",
  46.           url: url,
  47.           data: {
  48.             'action': 'appp_push_device_id',
  49.             'device_id': device_id,
  50.           },
  51.           success: function( response ) {
  52.             // alert(response.data);
  53.           }
  54.         });
  55.     };
  56.  
  57.     push.tokenHandler = function(result) {
  58.         // Your iOS push server needs to know the token before it can push to this device
  59.         // here is where you might want to send it the token for later use.
  60.         if(!PushWoosh) {
  61.             push.log('No PushWoosh object present');
  62.             return;
  63.         }
  64.         // PushWoosh Application ID, looks like this 40F80-E43E5
  65.         PushWoosh.appCode = apppPushVars.app_code;
  66.  
  67.         PushWoosh.register(result, function(data) {
  68.             push.log("PushWoosh register success: " + JSON.stringify(data));
  69.         }, function(errorregistration) {
  70.             push.log("Couldn't register with PushWoosh" +  errorregistration);
  71.         });
  72.         push.log("Token Handler " + result);
  73.     };
  74.  
  75.     push.errorHandler = function(error) {
  76.         push.log( "Error Handler  " + error );
  77.         // alert(error);
  78.     };
  79.  
  80.     /**
  81.      * result contains any message sent from the plugin call
  82.      * @since  1.0.0
  83.      * @param  {mixed} result Success result
  84.      */
  85.     push.successHandler = function(result) {
  86.         push.log( 'Success! Result = ' + result );
  87.     };
  88.  
  89.     /**
  90.      * Update DOM on a Received Event
  91.      * @since  1.0.0
  92.      */
  93.     push.receivedEvent = function(id) {
  94.  
  95.         var pushNotification = window.plugins.pushNotification;
  96.  
  97.         // Unregister before registering again
  98.         pushNotification.unregister(push.successHandler, push.errorHandler);
  99.  
  100.         // TODO: Enter your own GCM Sender ID in the register call for Android
  101.         if (device.platform == 'android' || device.platform == 'Android') {
  102.             push.log( 'GCM ID: ' + apppPushVars.gcm_id );
  103.             pushNotification.register(push.successHandler, push.errorHandler,{"senderID":apppPushVars.gcm_id,"ecb":"appp_push.onNotificationGCM"});
  104.         }
  105.         else {
  106.             pushNotification.register(push.tokenHandler,push.errorHandler,{"badge":"true","sound":"true","alert":"true","ecb":"appp_push.onNotificationAPN"});
  107.         }
  108.     };
  109.  
  110.     /**
  111.      * iOS
  112.      * @since  1.0.0
  113.      */
  114.     push.onNotificationAPN = function(event) {
  115.         // push.log('onNotificationAPN');
  116.         var pushNotification = window.plugins.pushNotification;
  117.         var pushTitle;
  118.        
  119.         if( apppPushVars.notifications_title ) {
  120.           // otherwise use setting
  121.           pushTitle = apppPushVars.notifications_title;
  122.         } else {
  123.           pushTitle = 'Notification';
  124.         }
  125.  
  126.         if ( event.alert ) {
  127.  
  128.           //var string1 = JSON.stringify( event );
  129.           // alert( event.u );
  130.  
  131.             navigator.notification.alert(
  132.                 event.alert,  // message
  133.                 null,         // callback
  134.                 pushTitle,            // title
  135.                 'Done'                // buttonName
  136.             );
  137.  
  138.         }
  139.         if (event.badge) {
  140.             // alert("Set badge on  " + pushNotification);
  141.             pushNotification.setApplicationIconBadgeNumber(push.successHandler, event.badge);
  142.         }
  143.         if (event.sound) {
  144.             var snd = new Media(event.sound);
  145.             snd.play();
  146.         }
  147.     };
  148.  
  149.     /**
  150.      * Android
  151.      * @since  1.0.0
  152.      */
  153.     push.onNotificationGCM = function(e) {
  154.  
  155.         var pushTitle;
  156.  
  157.         if( apppPushVars.notifications_title ) {
  158.           pushTitle = apppPushVars.notifications_title;
  159.         } else {
  160.           pushTitle = 'Notification';
  161.         }
  162.  
  163.         switch( e.event )
  164.         {
  165.             case 'registered':
  166.                 if ( e.regid.length > 0 )
  167.                 {
  168.  
  169.                 /* http://www.pushwoosh.com/programming-push-notification/android/android-additional-platforms/phonegap-build-generic-plugin-integration/ */
  170.                 PushWoosh.appCode = apppPushVars.app_code;
  171.                 PushWoosh.register(e.regid, function(data) {
  172.                     push.log("PushWoosh register success: " + JSON.stringify(data));
  173.                     }, function(errorregistration) {
  174.                     push.log("Couldn't register with PushWoosh" +  errorregistration);
  175.                 });
  176.                     // Your GCM push server needs to know the regID before it can push to this device
  177.                     // here is where you might want to send it the regID for later use.
  178.                     push.log('Registration id = '+e.regid);
  179.                 }
  180.  
  181.             break;
  182.  
  183.             case 'message':
  184.               // this is the actual push notification. its format depends on the data model
  185.               // of the intermediary push server which must also be reflected in GCMIntentService.java
  186.  
  187.               // Title for android header still not working when app is closed. Title not being received, may have to do with push plugin
  188.  
  189.               navigator.notification.alert(
  190.                   e.message,  // message
  191.                   null,         // callback
  192.                   pushTitle,            // title
  193.                   'Done'                // buttonName
  194.               );
  195.               // Not sure what msgcnt is
  196.               push.log('msgcnt = '+e.msgcnt);
  197.             break;
  198.  
  199.             case 'error':
  200.               push.log('GCM error = '+e.msg);
  201.             break;
  202.  
  203.             default:
  204.               push.log('An unknown GCM event has occurred');
  205.               break;
  206.         }
  207.     };
  208.  
  209.     /* Callback for push notifications
  210.      * Use for url redirects
  211.      */
  212.  
  213.     // push.notificationCallback = function(event) {
  214.     //   if(event.l) {
  215.     //     var url = l;
  216.     //     window.apppresser.loadAjaxContent( url, false, null );
  217.     //   }
  218.     // }
  219.  
  220.     push.log = function() {
  221.         if ( apppCore.log ) {
  222.             apppCore.log( arguments );
  223.         }
  224.     };
  225.  
  226.     push.initialize();
  227.  
  228.     return push;
  229.  
  230. })(window, document);

Raw Paste


Login or Register to edit or fork this paste. It's free.