Can I use GCM for a chrome EXTENSION? - google-chrome

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.

Related

Using Gmail API in Chrome Extension without Identity API?

I have a Chrome extension that uses the Gmail REST API to send emails on behalf of the user. This API requires an Oauth2 token, which I'm retrieving using chrome.identity.getAuthToken.
The Problem
However, I am running into some issues with the Chrome Identity API. In particular, if the user authenticates with chrome.identity with a different Gmail account then the one they're signed into Chrome with, then they are prompted to re-login every hour or so (which doesn't happen if the accounts are the same). In addition, I'd like to minimize the number of permissions my extension asks for as a general principle (permissions sometimes introduce warning messages on install and risk disabling existing users on update), so I'd like to not have to ask for the "identity" permission if I can avoid it.
My Question
How can I authenticate the Gmail API in Chrome Extensions without using the Identity API?
Current Progress
I initially tried using Google's Javascript Client for auth, but that seems to be incompatible with Chrome extensions. After having searched other SO issues and some Google materials, it seems that the Identity API is indeed the recommended auth solution in Chrome Extensions. However, for the UX reasons mentioned above, I'm finding this solution problematic. And I do think an alternative should be possible -- for example, the MixMax Chrome extension, which uses the Gmail API, does not ask for the Identity permission.
Any help would be much appreciated! Thanks!

Best way to send push notifications from the server to a chrome extension?

I'm making a chrome extension for a classified ads website. With the chrome extension users will be able to get latest lists from their watched categories/search query.
I am already working on a websocket server to send notifications to users, but this way I have to always have a socket connection open to every user.
My second approach was to use Firebase, but this will run the notification only when a user clicks the icon (as I've read), and I would rather have this done from my server
__
Are there any other possible solutions to send notifications to a chrome extension without the user clicking on the icon?
You can use Google's push infrastructure ― the very same that powers Android push notifications ― Google Cloud Messaging.
Note: Firebase Cloud Messaging is presented as an upgraded version of GCM; while true, it's not natively supported by Chrome in a way that GCM is.
chrome.gcm API is the one that works with it. See its documentation, as well as GCM documentation, for details.
There's even a tutorial: Implementing GCM Client on Chrome
But in a nutshell, your extension will register as a subscriber with GCM, pass the subscription ID to the server, and then the application server posts messages to GCM using those IDs.
You should also be able to use Firebase, if you're willing to implement it using the generic JS SDK; "this will run the notification only when a user clicks the icon" sounds pretty nonsensical ― a background page should be able to keep a listener alive and react, which is probably how your system works now. I would still recommend a native API, which should be compatible with Event pages.
Have you looked at the chrome.notifications API? It allows you to create rich notifications using templates and show these notifications to users in the system tray.
https://developer.chrome.com/apps/notifications
You can have a connection to your socket server in the background script, listen for messages from your socket server and trigger an event that shows the notification.

Web Push notifications on Chrome for Android: Do I need a third party service like GCM or SNS?

I'd like to set up Web Push notifications on my progressive web application (PWA) using Service Workers. But I'm having trouble understanding the role of third-party services like Google Cloud Messaging and Amazon SNS, and if I need them at all.
From what I understand, when the user clicks the Allow notifications button, you get a unique subscription endpoint. Then in your backend, you can use this endpoint to send notifications to that specific user.
However, all back end libraries that I've found (like pywebpush or web-push for Node.js), mention that you need a GCM API key in order to send notifications.
But here is what the MDN Push API documentation says:
Chrome versions earlier than 52 require you to set up a project on
Google Cloud Messaging to send push messages [...]
So I'm assuming that the new versions of Chrome (version 58 today) should be able to display notifications without the help of a third party.
Here is an example of what I want to achieve. I'm just not sure of what they do in the backend.
Note: I'm not trying to send native push notifications to Android or iOS devices, but only to my progressive web application on Chrome using the Web Push API.
The Browser Push Service
As I have explained in another answer, in order to send web push notifications, you need to interact with the browser push service. Basically each browser, when the user allows push notifications, returns an endpoint (URL) that is specific for its own push service.
For example:
Chrome and Opera endpoints start with prefix https://fcm.googleapis.com/, because they use FCM (ex GCM)
Firefox endpoints start with prefix https://updates.push.services.mozilla.com/, because Firefox uses Mozilla autopush
So notifications will always pass through FCM and autopush, there's no alternative: the push service is hardcoded inside the browser.
Web Push Notification Services as a layer of abstraction
There is also another kind of web push services. Their aim is to provide a layer of abstraction and additional features over the browser push service. For example Pushpad is one of them (I am the founder).
Instead of interacting directly with the different browser push services (e.g. FCM, autopush), your web app can interact just with one push service (e.g. Pushpad), which then interacts with the browser push services for you (automating many tasks, like VAPID and providing additional features like monitoring and integrations).
Confusion about FCM
FCM is quite confusing because it acts both as a "browser push service" for Chrome and Opera, but it also acts as a general "web push notification service" for other browsers. So for Firefox for example, FCM acts as a proxy towards Mozilla autopush.

google gcm "raw_data" payload for desktop chrome 50 beta

I am trying to send a and read message payload using the google GCM mechanism.
To send the data I am using the instructions found under "Web Push" in:
https://developers.google.com/web/updates/2016/03/web-push-encryption?hl=en
where it is instructed, that until full support by chrome is available, the payload can be sent using "raw_data" which needs to be base64 encripted.
to display the data I am ussing chorome 50Beta, since it is supposed to support payloads.
but when the 'push' event is fired, the service-worker event.data is null.
Is there a live web example of chrome push messaging payload out there?
You could use my web-push Node.js library. I've recently added support for Chrome 50.
I've just released the version 2 of the library with support for payloads on Chrome 50 and I've updated the ServiceWorker Cookbook to use it.
You can use the demos on that website (in particular the Push Payload one).

can i connect to an api after basic or digest authentication, in a google chrome extension

I want to connect to an API which uses Basic or Digest Auth- so the user can use the extension fully, only after he has logged in to the API- the extension will send and receive messages through the API and perform actions based on responses sent by the API.
Is such authentication possible for a google chrome extension? Can i ask user to provide userid/password (or API key) through a popup HTML File which is part of the extension? Or do I have to open a new tab in Chrome itself and ask the user to authenticate through that...
Which is the best/ideal way to authenticate into a web based API from a Google Chrome extension... Any guides or how-to s would be very helpful for me...
You can use popup or a new tab authentication, either way is OK.