Friday, 16 August 2013

How to call chrome.app.runtime in backgroundjs?

How to call chrome.app.runtime in backgroundjs?

this is my code in background.js to get me the channel id for push messages:
chrome.app.runtime.onLaunched.addListener(function(launchData) {
chrome.pushMessaging.getChannelId(true, channelIdCallback);
});
//This is called when the extension is installed.
chrome.runtime.onInstalled.addListener(function()
{
console.log("Push Messaging Sample Client installed!");
chrome.pushMessaging.getChannelId(true, channelIdCallback);
});
chrome.extension.onMessage.addListener(function(message, sender,
sendResponse) {
console.log(message)
});
//After Receiving Channel ID, listen for Push Messages
function channelIdCallback(message) {
console.log("Background Channel ID callback seen, channel Id is " +
message.channelId);
ListenForMessages();
//Show channel Id to user
chrome.app.window.create("PushHome.html?channelId="+message.channelId);
}
function ListenForMessages()
{
console.log("Listening for messages");
//Start listening for Push Messages.
chrome.pushMessaging.onMessage.addListener(messageReceived);
}
function messageReceived(message) {
var notification = window.webkitNotifications.createNotification(
'icon.jpg', 'Push Message',
message.payload +" [" + message.subchannelId + "]");
notification.show();
//Show it to user.
notification.show();
}
I get a Uncaught TypeError: Cannot read property 'onLaunched' of undefined
everytime I launch my application.
I understand that in my manifest I've defined it as browser_action and not
an "app".
But I'm creating an browser extension and not a chrome app. Hence how do I
do this and get rid of this error?

No comments:

Post a Comment