Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19.  
  20. var AppUrl = 'http://m.geaagronet.com';
  21. //var AppUrl = 'http://bd9b775b.ngrok.io/'; //localhost test
  22. var notificationData = '';
  23.  
  24. var app = {
  25.     // Application Constructor
  26.     initialize: function() {
  27.         document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
  28.         document.addEventListener('resume', this.onResume.bind(this), false);
  29.     },
  30.  
  31.     // deviceready Event Handler
  32.     //
  33.     // Bind any cordova events here. Common events are:
  34.     // 'pause', 'resume', etc.
  35.     onDeviceReady: function() {
  36.         initApp();
  37.     },
  38.    
  39.     onResume : function(){
  40.         initNotifications();
  41.     },
  42.  
  43.     // Update DOM on a Received Event
  44.     receivedEvent: function(id) {
  45.         var parentElement = document.getElementById(id);
  46.         var listeningElement = parentElement.querySelector('.listening');
  47.         var receivedElement = parentElement.querySelector('.received');
  48.  
  49.         listeningElement.setAttribute('style', 'display:none;');
  50.         receivedElement.setAttribute('style', 'display:block;');
  51.  
  52.         console.log('Received Event: ' + id);
  53.     }
  54.    
  55. };
  56.  
  57. app.initialize();
  58.  
  59.  
  60. function bindEvent(element, eventName, eventHandler) {
  61.     if (element.addEventListener) {
  62.         element.addEventListener(eventName, eventHandler, false);
  63.     } else if (element.attachEvent) {
  64.         element.attachEvent('on' + eventName, eventHandler);
  65.     }
  66. }
  67.  
  68.  
  69. var firstTime = true;
  70. function initApp(){
  71.    
  72.     //probaj da uzmez info sa SIM kartice i salji aplikaciji
  73.     if(firstTime){
  74.         if (device.platform == "Android")
  75.             window.plugins.sim.requestReadPermission(successReadPermission, errorReadPermission);
  76.         firstTime = false;
  77.     }
  78.    
  79.     // ako telefon nema pristup internetu
  80.     if (navigator) {
  81.         if (navigator.onLine == false) {
  82.             alert("Trenutno nemate pristup internetu. Proverite vašu internet konekciju i pokušajte ponovo.");
  83.             navigator.app.exitApp();
  84.         }
  85.     }
  86.  
  87.  
  88.     // ako je stari telefon koji ne podrzava IFrame:
  89.     if (device.platform == "Android") {
  90.         if (parseFloat(device.version)) {
  91.             if (parseFloat(device.version) < 4.3)
  92.                 window.location = AppUrl;
  93.         }
  94.     }
  95.    
  96.    
  97.     //postavi IFrame url
  98.     document.getElementById('contentFrame').src = AppUrl;
  99.  
  100.  
  101.     //uzmi token za notifikacije i posalji ga aplikaciji (ovde salje i uuid)
  102.     initNotifications();
  103.    
  104.    
  105.     // Listen to message from child window
  106.     bindEvent(window, 'message', function (e) {
  107.         var msg = JSON.parse(e.data);
  108.         if (msg && msg.type && msg.type == "open-link") {
  109.             var url = msg.data;
  110.             window.open(url, "_system");
  111.             if (device.platform == "iOS") {                      
  112.                 document.getElementById('contentFrame').contentWindow.history.go(-1);                      
  113.                 return;
  114.             }
  115.         }
  116.     });
  117.     this.receivedEvent('deviceready');
  118. }
  119.  
  120. function simInfoSuccess(result) {
  121.     if(result && result.cards && result.cards.length > 0 && result.cards[0].simSerialNumber){
  122.         $.get(AppUrl + "/Auth/AddToSession?key=simSerial&value=" + result.cards[0].simSerialNumber, function(data, status){});
  123.     }
  124.     if(result && result.cards && result.cards.length > 0 && result.cards[0].phoneNumber)
  125.         $.get(AppUrl + "/Auth/AddToSession?key=phoneNumber&value=" + result.cards[0].simSerialNumber, function(data, status){});
  126. }
  127.  
  128. function simInfoError(error) {
  129.     console.log("simInfoError");
  130. }
  131.  
  132. function successReadPermission(param) {
  133.     window.plugins.sim.getSimInfo(simInfoSuccess, simInfoError);
  134. }
  135.  
  136. function errorReadPermission(){
  137.     console.log("errorReadPermission");
  138. }
  139.  
  140. function initNotifications(){
  141.     $.get(AppUrl + "/Auth/AddToSession?key=uuid&value=" + device.uuid, function(data, status){});
  142.    
  143.     const push = PushNotification.init({
  144.           android: {},
  145.           browser: {},
  146.           ios: {},
  147.           windows: {}
  148.         });
  149.    
  150.     push.on('registration', data => {
  151.         $.get(AppUrl + "/Auth/AddToSession?key=notificationToken&value=" + data.registrationId, function(data, status){});
  152.     });
  153.    
  154.     push.on('notification', (data) => {
  155.         if(data && data.additionalData && data.additionalData.onOpenUrl){
  156.             document.getElementById('contentFrame').src = AppUrl + data.additionalData.onOpenUrl;
  157.         }
  158.            
  159.     });
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement