Skip to content

Commit

Permalink
Merge pull request #158 from LetterboxDev/duplicate-notif
Browse files Browse the repository at this point in the history
Move chrome notifications into a service. Closes #153.
  • Loading branch information
xbili committed Nov 12, 2015
2 parents bd82e55 + 38ba8b1 commit d52c481
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions www/js/controllers/appController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ angular.module('letterbox.controllers')
ChatService,
AuthService,
PushService,
ChromeNotifService,
VibrateService) { // Leave VibrateService here to init the service

$scope.username = window.localStorage.getItem('firstName') ? window.localStorage.getItem('firstName') : '';
Expand Down
24 changes: 0 additions & 24 deletions www/js/controllers/chatController.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,10 @@ angular.module('letterbox.controllers')
eventbus.registerListener('roomMessage', function(roomMessage) {
var message = roomMessage.message;

if (!window.cordova) {
var Notification = window.Notification || window.mozNotification || window.webkitNotification;
Notification.requestPermission(function (permission) {
});
}

function show(title, message) {
var instance = new Notification(
title, {
body: message,
icon: "img/android-icon-48x48.png"
}
);

instance.onshow = function () {
window.setTimeout(function(){ instance.close(); }, 10000);
};

return false;
}

if (message.RoomHash === $scope.roomHash) {
var formattedMessage = ChatService.formatMessage(message);
$scope.messages.push(formattedMessage);
$scope.$apply();
if (!window.cordova && !formattedMessage.isOwner) {
show($scope.recipient, formattedMessage.content);
}
$ionicScrollDelegate.scrollBottom(true);
}
});
Expand Down
41 changes: 41 additions & 0 deletions www/js/services/chromeNotifService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
angular.module('letterbox.services')

.service('ChromeNotifService', function($timeout, eventbus, ChatService) {
var ChromeNotif = this;

if (!window.cordova) {
var Notification = window.Notification ||
window.mozNotification ||
window.webkitNotification;

if (Notification.permission !== 'granted') {
Notification.requestPermission();
}
}

eventbus.registerListener('roomMessage', function(roomMessage) {
var message = roomMessage.message;
var senderName = roomMessage.senderName;
var formattedMessage = ChatService.formatMessage(message);

if (!window.cordova && !formattedMessage.isOwner) {
show(senderName, formattedMessage.content);
}
});

function show(title, message) {
var instance = new Notification(
title, {
body: message,
icon: "img/android-icon-48x48.png"
}
);

instance.onshow = function () {
$timeout(function(){
instance.close();
}, 10000);
};
}
});

0 comments on commit d52c481

Please sign in to comment.