From ec7e570aae73a837820ca71a4bfdd684c7572f27 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Sun, 6 Jul 2014 17:54:22 +0400 Subject: [PATCH] Basic PUSH support Added registerDevice for push updates. Closes #363 Fixed notification click. Closes #365 --- app/js/lib/config.js | 1 + app/js/lib/mtproto.js | 34 +++++++++++++++++++++++++++++++--- app/js/services.js | 28 +++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/app/js/lib/config.js b/app/js/lib/config.js index 266f8cf0..26fad054 100644 --- a/app/js/lib/config.js +++ b/app/js/lib/config.js @@ -36,6 +36,7 @@ Config.Navigator = { osX: (navigator.platform || '').toLowerCase().indexOf('mac') != -1 || (navigator.userAgent || '').toLowerCase().indexOf('mac') != -1, retina: window.devicePixelRatio > 1, + ffos: navigator.userAgent.match(/mobi.+Gecko/i), touch: screen.width <= 768, mobile: screen.width < 480 }; diff --git a/app/js/lib/mtproto.js b/app/js/lib/mtproto.js index 19e6f514..b623e64d 100644 --- a/app/js/lib/mtproto.js +++ b/app/js/lib/mtproto.js @@ -582,6 +582,10 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) $rootScope.offline = true; $rootScope.offlineConnecting = true; } + + if (Config.Navigator.mobile) { + this.setupMobileSleep(); + } }; MtpNetworker.prototype.updateSession = function () { @@ -596,6 +600,26 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) } }; + MtpNetworker.prototype.setupMobileSleep = function () { + var self = this; + $rootScope.$watch('idle.isIDLE', function (isIDLE) { + if (isIDLE) { + self.sleepAfter = tsNow() + 30000; + } else { + delete self.sleepAfter; + self.checkLongPoll(); + } + }); + + $rootScope.$on('push_received', function () { + // console.log(dT(), 'push recieved', self.sleepAfter); + if (self.sleepAfter) { + self.sleepAfter = tsNow() + 30000; + self.checkLongPoll(); + } + }); + }; + MtpNetworker.prototype.updateSentMessage = function (sentMessageID) { var sentMessage = this.sentMessages[sentMessageID]; if (!sentMessage) { @@ -716,14 +740,18 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) MtpNetworker.prototype.checkLongPoll = function(force) { var isClean = this.cleanupSent(); - // console.log('Check lp', this.longPollPending, tsNow()); + // console.log('Check lp', this.longPollPending, tsNow(), this.dcID, isClean); if (this.longPollPending && tsNow() < this.longPollPending || this.offline) { return false; } var self = this; Storage.get('dc').then(function (baseDcID) { - if (isClean && (baseDcID != self.dcID || self.upload)) { - // console.warn('send long-poll for guest DC is delayed', self.dcID); + if (isClean && ( + baseDcID != self.dcID || + self.upload || + self.sleepAfter && tsNow() > self.sleepAfter + )) { + // console.warn(dT(), 'Send long-poll for DC is delayed', self.dcID, self.sleepAfter); return; } self.sendLongPoll(); diff --git a/app/js/services.js b/app/js/services.js index d37d6449..4b7280e5 100644 --- a/app/js/services.js +++ b/app/js/services.js @@ -3226,6 +3226,20 @@ angular.module('myApp.services', []) } }); + var registeredDevice = false; + if (window.navigator.mozSetMessageHandler) { + window.navigator.mozSetMessageHandler('push', function(e) { + console.log(dT(), 'received push', e); + $rootScope.$broadcast('push_received'); + }); + + window.navigator.mozSetMessageHandler('push-register', function(e) { + console.log(dT(), 'received push', e); + registeredDevice = false; + registerDevice(); + }); + } + return { start: start, notify: notify, @@ -3305,6 +3319,11 @@ angular.module('myApp.services', []) return false; } + // FFOS Notification blob src bug workaround + if (Config.Navigator.ffos) { + data.image = 'https://raw.githubusercontent.com/zhukov/webogram/master/app/img/icons/icon60.png'; + } + notificationsCount++; if (!notificationsUiSupport || @@ -3333,9 +3352,9 @@ angular.module('myApp.services', []) notification.onclick = function () { notification.close(); - if (window.mozApps && document.hidden) { + if (window.navigator.mozApps && document.hidden) { // Get app instance and launch it to bring app to foreground - window.mozApps.getSelf().onsuccess = function() { + window.navigator.mozApps.getSelf().onsuccess = function() { this.result.launch(); }; } else { @@ -3391,15 +3410,13 @@ angular.module('myApp.services', []) notificationsShown = {}; } - var registeredDevice = false; - function registerDevice () { if (registeredDevice) { return false; } if (navigator.push) { var req = navigator.push.register(); - + req.onsuccess = function(e) { registeredDevice = req.result; MtpApiManager.invokeApi('account.registerDevice', { @@ -3430,6 +3447,7 @@ angular.module('myApp.services', []) registeredDevice = false; }) } + })