Can't register Chrome app for GCM - google-chrome

I'm trying to register a chrome app to get a registration id so I test but the chrome javascript console is showing:
Uncaught TypeError: Cannot read property 'register' of undefined
I don't know what's wrong. I got the example from: https://github.com/GoogleChrome/chrome-app-samples#push-messaging

Seems your Chrome APIs are not loaded.
You need to make sure your Chrome APIs are loaded in order to use the push messaging methods.
In order to load the Chrome APIs correctly, follow the step 5 in Chrome developer page to launch your app or extension. For some newer Chrome browser, you can go to Settings -> Extensions -> Load unpacked extension

Had the same problem,
Eventually the problem was that content scripts cannot use a lot of chrome.* APIs.
GCM is one of them.
Source

Related

Can I use GCM for a chrome EXTENSION?

I'm new to web development and currently I'm trying to create a chrome extension.
I followed this google tutorial and also this one and this one but for some reason the push notifications does not work when I define the extension as an extension and not as an application.
Can anyone give some guidance? my code is basically identical to the 2nd tutorial only I am defining the extensions as an extension and not as an application.
To my understanding it might be impossible due to safety reasons(?)..
Any help will be appreciated...
Can I use GCM for a chrome EXTENSION?
Yes you can. GCM docs states that:
For Chrome apps and extensions, GCM enables Chrome instances to send
and receive message data from servers. The
chrome.gcm API allows the
Chrome apps or extensions to access the GCM service. The service works
even if an app or extension isn't currently running. For example,
calendar updates could be pushed to users even when their calendar app
isn't open.
Receive downstream messages
chrome.gcm.onMessage.addListener(function(message) {
// A message is an object with a data property that
// consists of key-value pairs.
});
There's also a dev blog guide that you might want to see. It includes sample codes and instructions like:
How push messaging works
1) Chrome extension gets a Channel ID from Chrome Run-time
2) After receiving Channel ID, your extension sends the channel ID to your web server.
3) Using GCM API, you can send a message to chrome browser which will queued in GCM Server.
4) GCM server sends your message to Chrome Run-time.
5) Chrome Run-time forwards the message to your extension.

Chrome Extension equivalent to Android's INSTALL_REFERRER

Is there a way to detect where a chrome extension installation was referred from. In case you want to rebrand the extension based on where the user came from
Android Apps are capable of this using intent com.android.vending.INSTALL_REFERRER and a referrer=something querystring on their Google Play URL, but I've been unable to find anything similar for Chrome Extensions.
I can probably do a 80% solution with cookies and redirects but it would be nice if this worked as cleanly as in Android INSTALL_REFERRER
It seems that there is no referrer option for Chrome Extensions and it is not listed in the documentation. See the following link for the closest I've found.
https://developer.chrome.com/extensions/runtime#type-OnInstalledReason
A workaround for this would be to pull the data from Google Analytics using the API. Referrals are recorded in Google Analytics for my Chrome Extension:

Is there a way to launch chrome packaged app from chrome extension

I'm developing a chrome packaged app and chrome extension, both of them communicate with one another, and i want to lunch my packaged app from a chrome extension, is there any way to do it?
Note:
I've tried using the launchApp method of chrome management api, but for some unknown reason the chrome.management is undefined in my chrome extension JS code, although i have specified management permission in my manifest file like so:
"permissions": ["management"]
Does anyone have a idea what is the problem, or there are any other way i can do it ?
Thanks for help:)
There are 2 possible reasons for not being able to use chrome.management.
You have not reloaded your extension properly
You are trying to call this from a content script; you can't do that, since a content script has very restricted access to Chrome API. You need to message a background page to do this for you.
That said, there is a better way to do it if you write both your extension and your app. "management" permission is a big hammer and will generate a warning to the user on installation.
Instead, you can send a cross-extension message to your app. It will wake it up and you can launch your main window from there. See this answer for details.

Chrome Packaged App Access Chrome APIs

I'm interested in writing a Packaged App that can access data about chrome, namely the chrome.windows.onCreated and chrome.windows.onRemoved events. If I try to add a listener to either of these, I get an error in the console:
Uncaught TypeError: Cannot read property 'onRemoved' of undefined
Is there any way around this?
The other answers are correct that this isn't directly possible from a packaged app, but there is a solution that meets your needs: You can write an extension as well as a packaged app and have them communicate with chrome.runtime.sendMessage.
The user will have to install both app and extension, but you can make this easy by directing them to the chrome web store from within your application. You can read about this here: Communicating between a Chrome packaged app and a Chrome extension?
Edit: and as pointed out in a comment on that thread, there is a Chrome App Sample that helps demonstrate this in action: https://github.com/GoogleChrome/chrome-app-samples/tree/master/messaging
The chrome.windows API is a Chrome extension API, not a packaged apps API. It is used by extensions to interact with browser windows.
If you can write your application as an extension, it will be able to use that API. Packaged apps, however, don't have the ability to manipulate other windows besides their own.
One simple typo here: It is chrome.windows.onRemoved not chrome.windows.OnRemoved ;)
Note the lowercase o.

Can you still create a Chrome packaged app in the browser with manifest 2.0?

All of the examples I've seen show apps launching in their own windows. This may be great an all for chromebook/chrome os, but is there still an option to launch in a browser tab?
No, there is no way to do that. Chrome Packaged Apps are not supposed to run inside a browser. You can, however, open URLs in a browser tab using window.open. But you won't have control of that tab after you issue the command.
If you need some sort of integration/control between your Chrome Packaged App and the browser, you can create an extension and make a communication pipe between the extension and the app - as long as both are running, using the chrome.runtime.sendMessage API.
See this sample for a simple code that does exactly that (two apps and one extension exchanging messages directly, without any server component).
chrome.app.window.create will create a new Window for an App.
If you want window manipulation, you should switch to chrome.tabs API and look for an extension instead of an App.
Reference
chrome.tabs
chrome.app.window