Browse Source

Basic PUSH support

Added registerDevice for push updates. Closes #363
Fixed notification click. Closes #365
master
Igor Zhukov 11 years ago
parent
commit
ec7e570aae
  1. 1
      app/js/lib/config.js
  2. 34
      app/js/lib/mtproto.js
  3. 26
      app/js/services.js

1
app/js/lib/config.js

@ -36,6 +36,7 @@ Config.Navigator = {
osX: (navigator.platform || '').toLowerCase().indexOf('mac') != -1 || osX: (navigator.platform || '').toLowerCase().indexOf('mac') != -1 ||
(navigator.userAgent || '').toLowerCase().indexOf('mac') != -1, (navigator.userAgent || '').toLowerCase().indexOf('mac') != -1,
retina: window.devicePixelRatio > 1, retina: window.devicePixelRatio > 1,
ffos: navigator.userAgent.match(/mobi.+Gecko/i),
touch: screen.width <= 768, touch: screen.width <= 768,
mobile: screen.width < 480 mobile: screen.width < 480
}; };

34
app/js/lib/mtproto.js

@ -582,6 +582,10 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
$rootScope.offline = true; $rootScope.offline = true;
$rootScope.offlineConnecting = true; $rootScope.offlineConnecting = true;
} }
if (Config.Navigator.mobile) {
this.setupMobileSleep();
}
}; };
MtpNetworker.prototype.updateSession = function () { 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) { MtpNetworker.prototype.updateSentMessage = function (sentMessageID) {
var sentMessage = this.sentMessages[sentMessageID]; var sentMessage = this.sentMessages[sentMessageID];
if (!sentMessage) { if (!sentMessage) {
@ -716,14 +740,18 @@ angular.module('izhukov.mtproto', ['izhukov.utils'])
MtpNetworker.prototype.checkLongPoll = function(force) { MtpNetworker.prototype.checkLongPoll = function(force) {
var isClean = this.cleanupSent(); 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) { if (this.longPollPending && tsNow() < this.longPollPending || this.offline) {
return false; return false;
} }
var self = this; var self = this;
Storage.get('dc').then(function (baseDcID) { Storage.get('dc').then(function (baseDcID) {
if (isClean && (baseDcID != self.dcID || self.upload)) { if (isClean && (
// console.warn('send long-poll for guest DC is delayed', self.dcID); 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; return;
} }
self.sendLongPoll(); self.sendLongPoll();

26
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 { return {
start: start, start: start,
notify: notify, notify: notify,
@ -3305,6 +3319,11 @@ angular.module('myApp.services', [])
return false; 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++; notificationsCount++;
if (!notificationsUiSupport || if (!notificationsUiSupport ||
@ -3333,9 +3352,9 @@ angular.module('myApp.services', [])
notification.onclick = function () { notification.onclick = function () {
notification.close(); notification.close();
if (window.mozApps && document.hidden) { if (window.navigator.mozApps && document.hidden) {
// Get app instance and launch it to bring app to foreground // 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(); this.result.launch();
}; };
} else { } else {
@ -3391,8 +3410,6 @@ angular.module('myApp.services', [])
notificationsShown = {}; notificationsShown = {};
} }
var registeredDevice = false;
function registerDevice () { function registerDevice () {
if (registeredDevice) { if (registeredDevice) {
return false; return false;
@ -3430,6 +3447,7 @@ angular.module('myApp.services', [])
registeredDevice = false; registeredDevice = false;
}) })
} }
}) })

Loading…
Cancel
Save