[PYTHON] Send a message from the server to your Chrome extension using Google Cloud Messaging for Chrome

(As of 2013-07-09)

Already two months ago, Google announced Google Cloud Messaging, which sends push notifications to Chrome extensions and the like. Maripo wrote the following article at an early stage.

I tried Google Cloud Messaging for Chrome

I wrote a demo that sends a Channel ID from the Chrome extension to GAE and sends a message from GAE to the sent ID, so I will write it for the time being.

In this article, I will write what I noticed when I tried GCM. In addition, since the author's web-based work technology is low, it would be helpful if you could tell me about the points that can be stingy in the above repository.

I will write about the above two projects at the end.

Start by making an extension for the time being

I had made a Chrome extension for a certain matter before, but I started by doing it again based on the original tutorial.

Register your Chrome extension with the Chrome Web Store.

The extended registration itself has nothing to do with GCM at first glance, but it is used to get the channel ID. The channel ID is the destination that points to the Chrome extensions installed in each user's Chrome.

Click here for a quote from Honke:

Similar to an email address, the channel ID is used to identify and send messages to a specific user of your app or extension. Your app or extension needs to send this value to its application server so that the server can trigger push messages back.

I register on Google's server that realizes GCM, but at this time it costs $ 5 only once to register as a developer. It should also be effective when using APIs other than GCM. Most of the readers here think that this step was done a few years ago.

In the original document, there is also "Get the channel ID at install time using app.runtime.onLaunched." As a method to get the channel ID for the test, so I thought that I could experiment without the above registration, but it seems to be useless. This description looks like a description about Packaged Apps in the first place. In other words, it doesn't seem to be accessible from Chrome extensions.

Then, upload the created extension as a zip. Of course, you can limit who can download. This time, I uploaded it in the form of Trusted Tester only == Virtually only myself.

https://chrome.google.com/webstore/developer/dashboard

I think it's basic, but when you upload a Chrome extension to Google Play, the changes don't take effect immediately. Specifically, it says:

The item is being published. It can take up to 60 minutes for it to appear on the Chrome Web Store.

Actually, it doesn't seem to take 60 minutes to be displayed, but it doesn't update in 1 second.

It's subtly annoying to always experience this lag during development, so it's a good idea to manually set the key as described later in Maripo's article. You can look up the key and manually embed it in manifest.json. If you are using google-chrome on Linux, there seems to be an extension under ~ / .config / google-chrome / Default / Extensions /, so you can pull out the key from it and put it in the one under development.

Make Chrome extension GCM compatible

There are four things the Chrome extension must do to receive a message:

I have no choice but to write permissions in manifest.json, so I will do so.

If "Prepare channel ID" is an extension registered in the Chrome Web Store, you can get it by hitting the appropriate API (chrome.pushMessaging.getChannelId ()).

You need to create the "tell the channel ID to the server" part by yourself according to the form of the service you want to provide in the end. I implemented it this time as if it were sent when the popup was opened.

"Implementing a callback to receive a message" is not in the tutorial, but it can be done naturally by using the background mechanism.

There was no implementation example that works for this part, so I wrote it just in case, but it was not a big deal. However, there is something like a habit peculiar to Chrome extension, so I learned that.

Register the project in the Google APIs Console so that you can request messages from the server side

The Google API Console finally gives you three pieces of information: These have no expiration date and seem to be usable forever.

I'm not sure why I need a refresh token in addition to my client ID / secret here. You may not understand it because you do not understand the mechanism of OAuth.

From these three sets, you get a time-limited access token. This access token, channel ID (destination), and payload (message body) actually send the message to a specific extension.

Once from the head family.

You need two types of OAuth 2.0 tokens to authorize each call to the push messaging service: the refresh token and the access token. The access token authorizes each call to the service; however, this token expires after about an hour. The refresh token is used to 'refresh' the access token over time. These tokens are scoped to only send messages on behalf of your application or extension and nothing else.

The first three must be obtained manually via the Google APIs Console in advance, and the access token must be obtained again by the server as appropriate.

Implement the server on GAE

If you use the curl command to send messages manually, you don't need a server. You can get an access token from three types of information, client ID, client secret, and refresh token, and send a message using the access token and channel ID.

I wanted to link Chrome extension with GAE, so I wrote the server side as appropriate.

About two projects

A button will be placed when you install the Chrome extension. When you press the button, the channel ID will be sent to the server. The server remembers the channel ID and can send a simple message to the browser extension that sent the channel ID.

In addition, when playing, you have to do at least the following work. It's a lot of trouble

It's also important because we don't give much consideration to security.

Recommended Posts

Send a message from the server to your Chrome extension using Google Cloud Messaging for Chrome
Send a message from Slack to a Python server
[LINE Messaging API] I want to send a message from the program to everyone's LINE
Send log data from the server to Splunk Cloud
Send a message from Python to Slack
Send an email from the VirtualBox CentOS 8 server using your Google account as the sending address and using the app password
I want to send a message from Python to LINE Bot
A Python script that allows you to check the status of the server from your browser
A story about switching a personally developed Web service from a rental server to GCP (Google Cloud Platform)
Send push notifications from your iOS app via Firebase Cloud Messaging
Post a message from IBM Cloud Functions to Slack in Python
I want to send a signal only from the sub thread to the main thread
Try using the Python web framework Django (1)-From installation to server startup
Connect your SQL Server database to Alibaba Cloud Function Compute using Python
How to run a quickstart for Google Cloud Text-to-Speech .NET in Unity
Implemented in Dataflow to copy the hierarchy from Google Drive to Google Cloud Storage
[python] Send the image captured from the webcam to the server and save it
How to build an application from the cloud using the Django web framework
Convert the cURL API to a Python script (using IBM Cloud object storage)
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Instructions for connecting Google Colab. To the local runtime in a Windows environment
Continue to challenge Cyma's challenges using the OCR service of Google Cloud Platform
Send a message to LINE with Python (LINE Notify)
How to send a message to LINE with curl
I tried using the Google Cloud Vision API
Write a TCP server using the SocketServer module
POST images from ESP32-CAM (MicroPython) to the server
[Python] Throw a message to the slack channel
A real way for people using python 3.8.0-2 from windows to work with multibyte characters
[For beginners] Language analysis using the natural language processing tool "GiNZA" (from morphological analysis to vectorization)
I want to set up a mock server for python-flask in seconds using swagger-codegen.
The story of switching from WoSign to Let's Encrypt for a free SSL certificate
A story about a Python beginner trying to get Google search results using the API
Send a push message to the LINE Bot when the LTE-M Button is pressed [SORACOM]
Find a guideline for the number of processes / threads to set in the application server