ESP8266 - GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

User avatar
beegee1962
Posts: 11
Joined: Fri Jan 08, 2016 11:11 am
Location: Manila, Philippines
Contact:

ESP8266 - GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

Postby beegee1962 » Mon Feb 01, 2016 5:33 pm

I was looking into a solution to send push notifications from an ESP8266 module to an Android application. I know that this is possible with the Google Cloud Messaging service.

But what I found on the internet is using an external webserver, able to run PHP (or Python) scripts and using a MySQL server to store registration IDs.
ImageImage
So far so good, but why do I need to setup an external server with MySQL and PHP just to send messages to the Android devices. The ESP8266 has server capabilities build-in and has enough storage to save registration IDs in the Flash memory.
What I wanted is:
ImageImage

As I could not find any tutorial or example on the Internet how to achieve this, I started to write the code by myself.
And here is the result:
ESP8266 - Google Cloud Messaging without external server
Of course the code is open source and stored in my Github repository

Hope it helps someone.

wuyunzhou
Posts: 12
Joined: Fri Apr 10, 2015 3:46 pm

Re: ESP8266 - GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

Postby wuyunzhou » Tue Feb 02, 2016 5:17 pm

good job.
yes, if the device can get the gcm api_key, and the android registration id, the device can notify without server.

so the first is to pass the android registration id from app to device,
then make a http request to Gcm server, like this:

POST /gcm/send HTTP/1.1
User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
Host: gcm-http.googleapis.com
Accept: */*
Authorization: key=${api_key}
Content-Type: application/json
Content-Length: xx

{"registration_ids":%s,"priority":"%s","time_to_live":%s,"data":%s}

registration_ids is a array with android registration ids.

User avatar
beegee1962
Posts: 11
Joined: Fri Jan 08, 2016 11:11 am
Location: Manila, Philippines
Contact:

Re: ESP8266 - GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

Postby beegee1962 » Tue Feb 02, 2016 8:26 pm

wuyunzhou,
Correct, that is how it works. However I didn't use the "priority" and "time_to_live' keys.

JamesRit
Posts: 2
Joined: Fri May 12, 2017 12:03 am
Location: Latvia
Contact:

ESP8266 GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

Postby JamesRit » Mon May 22, 2017 4:09 pm

Hi. copying the form of the sample in my test project, allways get the error google is not defined. I can run the sample avobe with no problem, but with my test project, allways the same error. I dont konw why. Thanks.

RalphKeil
Posts: 1
Joined: Mon Jul 26, 2021 5:46 pm

Re: ESP8266 - GOOGLE CLOUD MESSAGING WITHOUT EXTERNAL SERVER

Postby RalphKeil » Mon Jul 26, 2021 5:50 pm

I'm looking for a method to send notifications with Google Cloud Messaging using javascript from my pc, so without a middle server. As pointed in the Developer's web site, the format of the request should be like this:

Code: Select all

https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

{ "data": {
    "score": "5x1",
    "time": "15:10"
  },
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}


First I've tryed to use the ajax method:

Code: Select all

var textNotification={
    "notification": {
        "title": "Portugal vs. Denmark",
        "text": "5 to 1"
    }
};
    $.ajax(
    {
        url : "https://gcm-http.googleapis.com/gcm/send",
        type : "POST",
        headers :
        {
            'Authorization' : 'AIzaSyZ-1u...',
            'Content-type': 'application/json'
        },
        data : textNotification
    }
    ).done(function (data)
    {
        console.log(data);
    }
    );


As aspected, due to the fact I'm trying to send an HTTPS request from my PC, it is blocked. Is there another way to follow?

source : https://wasaelgroup.com

Who is online

Users browsing this forum: No registered users and 0 guests