Browse Source

Basic PUSH support

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

1
app/js/lib/config.js

@ -36,6 +36,7 @@ Config.Navigator = { @@ -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
};

34
app/js/lib/mtproto.js

@ -582,6 +582,10 @@ angular.module('izhukov.mtproto', ['izhukov.utils']) @@ -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']) @@ -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']) @@ -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();

28
app/js/services.js

@ -3226,6 +3226,20 @@ angular.module('myApp.services', []) @@ -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', []) @@ -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', []) @@ -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', []) @@ -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', []) @@ -3430,6 +3447,7 @@ angular.module('myApp.services', [])
registeredDevice = false;
})
}
})

Loading…
Cancel
Save