Skip to content
This repository has been archived by the owner on Oct 6, 2021. It is now read-only.

Commit

Permalink
Notifications sound support
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Jul 23, 2015
1 parent d38fffd commit b9d5cda
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"optionsMinutes": {
"message": "minute(s)"
},
"optionsCheckboxNotificationSound": {
"message": "Play sound"
},
"optionsTestNotifications": {
"message": "Send test notification"
},
Expand Down
3 changes: 3 additions & 0 deletions src/_locales/fr/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@
"optionsMinutes": {
"message": "minute(s)"
},
"optionsCheckboxNotificationSound": {
"message": "Emettre un son"
},
"optionsTestNotifications": {
"message": "Envoyer une notification de test"
},
Expand Down
23 changes: 20 additions & 3 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,16 @@
}

function showNotification() {
var sound = false;
if (!chrome.notifications || localStorage.notifIsActivated != 'true') {
return;
}
if (localStorage.notifIsSound == 'true') {
sound = true;
}
new NotificationsCount(function (count) {
if (count !== false && count > 0) {
renderNotification(count);
renderNotification(count, sound);
}
});
}
Expand Down Expand Up @@ -210,7 +214,7 @@
}

// notitifcation renderer
function renderNotification(count) {
function renderNotification(count, sound) {
var opt = {
type: "basic",
title: chrome.i18n.getMessage('notificationTitle'),
Expand All @@ -227,6 +231,11 @@
iconUrl: "icon-48.png"
};
var notification = chrome.notifications.create('showNotification', opt, function() {
if (sound) {
var notifAudio = new Audio();
notifAudio.src = 'knock_brush.mp3';
notifAudio.play();
}
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
if (chrome.runtime.lastError.message == "Adding buttons to notifications is not supported.") {
Expand Down Expand Up @@ -351,6 +360,7 @@
localStorage.iconShowIngame = true;
localStorage.notifIsActivated = false;
localStorage.notifFrequency = 5;
localStorage.notifIsSound = true;
} else if (details.reason == 'update') {
console.log(manifest.name + " updated from v" + details.previousVersion + " to v" + manifest.version);
}
Expand All @@ -367,7 +377,14 @@
showNotification();
break;
case 'shownotification_test':
renderNotification(Math.floor((Math.random()*10)+1));
var sound = false;
if (!chrome.notifications) {
return;
}
if (localStorage.notifIsSound == 'true') {
sound = true;
}
renderNotification(Math.floor((Math.random()*10)+1), sound);
break;
}
});
Expand Down
Binary file added src/knock_brush.mp3
Binary file not shown.
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "__MSG_extName__",
"description": "__MSG_extDescription__",
"version": "0.1.1",
"version": "0.1.2",
"background": {
"persistent": false,
"scripts": [
Expand Down
4 changes: 4 additions & 0 deletions src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ <h3 data-message="optionsSectionUpdatesHeader">Updates</h3>
</select>
<span data-message="optionsMinutes">minute(s)</span>.
</div>
<div class="checkbox">
<input type="checkbox" id="notifIsSound">
<label for="notifIsSound" data-message="optionsCheckboxNotificationSound">Play sound</label>
</div>
<div style="margin-top: 10px;">
<button id="notifTest" data-message="optionsTestNotifications">Send test notification</button>
</div>
Expand Down
12 changes: 12 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
var inputIconShowIngame = document.getElementById('iconShowIngame');
var inputNotifIsActivated = document.getElementById('notifIsActivated');
var inputNotifFrequency = document.getElementById('notifFrequency');
var inputNotifIsSound = document.getElementById('notifIsSound');
var successMessage = document.getElementById('success_message');
var successTimeout = null;

Expand Down Expand Up @@ -83,6 +84,12 @@
} else {
inputNotifFrequency.value = localStorage.getItem('notifFrequency');
}
// notifIsSound
if (localStorage.getItem('notifIsSound') === null) {
inputNotifIsSound.checked = true;
} else {
inputNotifIsSound.checked = (localStorage.getItem('notifIsSound') === 'true');
}
}
loadOptions();

Expand All @@ -94,6 +101,7 @@
localStorage.setItem('iconShowIngame', inputIconShowIngame.checked);
localStorage.setItem('notifIsActivated', inputNotifIsActivated.checked);
localStorage.setItem('notifFrequency', inputNotifFrequency.value);
localStorage.setItem('notifIsSound', inputNotifIsSound.checked);
// success message
clearTimeout(successTimeout);
successMessage.classList.add('visible');
Expand Down Expand Up @@ -125,6 +133,10 @@
saveOptions();
chrome.runtime.sendMessage({do: 'shownotification'});
});
document.getElementById('notifIsSound').addEventListener('change', function () {
saveOptions();
chrome.runtime.sendMessage({do: 'shownotification'});
});

// Notification test
document.getElementById('notifTest').addEventListener('click', function () {
Expand Down

0 comments on commit b9d5cda

Please sign in to comment.