-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
executable file
·55 lines (43 loc) · 1.4 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Returns a new notification ID used in the notification.
function getNotificationId() {
var id = Math.floor(Math.random() * 9007199254740992) + 1;
return id.toString();
}
var myId = "";
function messageReceived(message) {
console.log("Message received: " + message.data['id'] + ' / ' + message.data['message']);
// Set up my sessionId to distinguish the message sender
if(myId == ""){
myId = message.data['id'];
return;
}
// We do not need to do noti when this message is from this client.
if(myId == message.data['id'])
return;
// Pop up a notification to show the GCM message.
chrome.notifications.create(getNotificationId(), {
title: 'MSP Chat CodeLab',
iconUrl: 'gcm_128.png',
type: 'basic',
message: message.data['message']
}, function() {});
}
var registerWindowCreated = false;
function firstTimeRegistration() {
chrome.storage.local.get("registered", function(result) {
registerWindowCreated = true;
chrome.app.window.create(
"register.html",
{ width: 400,
height: 600,
frame: 'chrome'
},
function(appWin) {}
);
});
}
// Set up a listener for GCM message event.
chrome.gcm.onMessage.addListener(messageReceived);
// Set up listeners to trigger the first time registration.
chrome.runtime.onInstalled.addListener(firstTimeRegistration);
chrome.runtime.onStartup.addListener(firstTimeRegistration);