我为chrome写了一个扩展gcm通知。当我在chrome上登录帐户gmail时,首先,我可以打开扩展名正常。我关闭扩展应用程序,我重新打开扩展应用程序,但应用程序无法显示。当我在chrome上注销帐户gmail时,我可以关闭打开正常的应用程序。我不知道为什么。
Background.js
// Returns a new notification ID used in the notification.
var registerWindowCreated = false;
function getNotificationId() {
var id = Math.floor(Math.random() * 9007199254740992) + 1;
return id.toString();
}
function messageReceived(message) {
// A message is an object with a data property that
// consists of key-value pairs.
// Concatenate all key-value pairs to form a display string.
var messageString = "";
for (var key in message.data) {
if (messageString != "")
messageString += ", "
messageString += message.data[key];
//messageString += key + ":" + message.data[key];
}
console.log("Message received: " + messageString);
// Pop up a notification to show the GCM message.
chrome.notifications.create(getNotificationId(), {
title: 'NETWORK MONITOR',
iconUrl: 'gcm_128.png',
type: 'basic',
message: messageString
}, function() {
if(registerWindowCreated==false)
{
chrome.app.window.create(
"index.html",
{ width: 800,
height: 600,
frame: 'chrome'
},
function(appWin) {}
);
}
});
}
function firstTimeRegistration() {
chrome.storage.local.get("registered", function(result) {
// If already registered, bail out.
if (result["registered"])
return;
registerWindowCreated = true;
chrome.app.window.create(
"index.html",
{ width: 800,
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.app.runtime.onLaunched.addListener(firstTimeRegistration);
chrome.runtime.onInstalled.addListener(firstTimeRegistration);
chrome.runtime.onStartup.addListener(firstTimeRegistration);
的manifest.json
{
"name": "Network Monitor",
"description": "Chrome platform app.",
"manifest_version": 2,
"version": "1.0",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": ["gcm", "storage", "notifications","http://*.longvan.net/","webview"],
"icons": { "128": "gcm_128.png" }
}