From 19a7883f233a9ff43e6585732ae8f7e7a2ae06f2 Mon Sep 17 00:00:00 2001 From: Arjun Roychowdhury Date: Thu, 22 Oct 2015 16:12:44 -0400 Subject: make sure push is called on init if not done before, also simplified objects in receive notification --- www/js/EventServer.js | 167 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 123 insertions(+), 44 deletions(-) (limited to 'www/js/EventServer.js') diff --git a/www/js/EventServer.js b/www/js/EventServer.js index c49eae02..e47ca109 100644 --- a/www/js/EventServer.js +++ b/www/js/EventServer.js @@ -2,7 +2,7 @@ /* jslint browser: true*/ -/* global cordova,StatusBar,angular,console */ +/* global cordova,StatusBar,angular,console ,PushNotification*/ //-------------------------------------------------------------------------- // This factory interacts with the ZM Event Server @@ -11,8 +11,8 @@ angular.module('zmApp.controllers') -.factory('EventServer', ['ZMDataModel', '$rootScope', '$websocket', '$ionicPopup', '$cordovaLocalNotification', '$cordovaBadge', '$timeout', '$q', 'zm', '$ionicPlatform', function - (ZMDataModel, $rootScope, $websocket, $ionicPopup, $cordovaLocalNotification, $cordovaBadge, $timeout, $q, zm, $ionicPlatform) { +.factory('EventServer', ['ZMDataModel', '$rootScope', '$websocket', '$ionicPopup', '$timeout', '$q', 'zm', '$ionicPlatform', function + (ZMDataModel, $rootScope, $websocket, $ionicPopup, $timeout, $q, zm, $ionicPlatform, $cordovaBadge) { var ws; @@ -67,14 +67,19 @@ angular.module('zmApp.controllers') }); if ($rootScope.apnsToken != '') { + var plat = $ionicPlatform.is('ios') ? 'ios':'android'; + ws.$emit('push', { type: 'token', - platform: 'ios', + platform: plat, token: $rootScope.apnsToken }); } } + + + @@ -95,6 +100,10 @@ angular.module('zmApp.controllers') d.reject("false"); return d.promise; } + + if (!$rootScope.apnsToken) + pushInit(); + if (typeof ws !== 'undefined') { @@ -194,40 +203,12 @@ angular.module('zmApp.controllers') } } - } else { - ZMDataModel.zmDebug("App is in background, displaying localNotification"); - - - if (localNotificationId == 0) { - localNotificationId = 1; - ZMDataModel.zmDebug("Creating notification ID " + localNotificationId + " with " + localNotText); - $cordovaLocalNotification.schedule({ - id: 1, - title: $rootScope.alarmCount + ' new ZoneMinder Alarms', - text: localNotText, - sound: "file://sounds/blop.mp3" - - }).then(function (result) { - // do nothing for now - }); - } else { - ZMDataModel.zmDebug("Updating notification ID " + localNotificationId + " with " + localNotText); - $cordovaLocalNotification.update({ - id: 1, - title: $rootScope.alarmCount + ' new ZoneMinder Alarms', - text: localNotText, - - }).then(function (result) { - // do nothing for now - }); - } - - } + } - if (!$ionicPlatform.is('ios') ) + /* if (!$ionicPlatform.is('ios') ) { // this is only used for local notifications which is not - // used for + // used for iOS // lets set badge of app irrespective of background or foreground $cordovaBadge.hasPermission().then(function (yes) { @@ -244,9 +225,7 @@ angular.module('zmApp.controllers') }, function (no) { ZMDataModel.zmDebug("zmNinja does not have badge permissions. Please check your phone notification settings"); }); - } - - + }*/ } //end of success handler @@ -305,10 +284,6 @@ angular.module('zmApp.controllers') - - - - } //-------------------------------------------------------------------------- @@ -352,14 +327,118 @@ angular.module('zmApp.controllers') } - - } + + function pushInit() + { + ZMDataModel.zmLog ("Setting up push registration"); + var push = PushNotification.init( + { "android": + {"senderID":zm.gcmSenderId} + }, + + { "ios": + {"alert": "true", + "badge": "true", + "sound": "true"} + } + + ); + + + + push.on('registration', function(data) { + ZMDataModel.zmDebug("Push Notification registration ID received: " + JSON.stringify(data)); + $rootScope.apnsToken = data.registrationId; + + var plat = $ionicPlatform.is('ios') ? 'ios':'android'; + + sendMessage('push', + { + type:'token', + platform:plat, + token:$rootScope.apnsToken}); + + + }); + + + push.on('notification', function(data) { + + var ld = ZMDataModel.getLogin(); + if (ld.isUseEventServer=="0") + { + ZMDataModel.zmDebug("received push notification, but event server disabled. Not acting on it"); + return; + } + console.log ("************* PUSH RECEIVED ******************"); + console.log (JSON.stringify(data)); + + // data.message, + // data.title, + // data.count, + // data.sound, + // data.image, + // data.additionalData + + if (data.additionalData.foreground == false) + { + // This means push notification tap in background + + ZMDataModel.zmDebug("**** NOTIFICATION TAPPED SETTING TAPPED TO 1 ****"); + $rootScope.alarmCount="0"; + $rootScope.isAlarm = 0; + $rootScope.tappedNotification = 1; + } + else + { + // alarm received in foregroun + //var str=data.additionalData.alarm_details; + var str = data.message; + // console.log ("***STRING: " + str + " " +str.status); + var eventsToDisplay=[]; + + /*console.log ("PUSH IS " + JSON.stringify(str.events)); + var alarmtext = ""; + for (var iter=0; iter