This is fork of gcm-erlang optimized to use with smoothie.
This software provides an Erlang client for Google Cloud Messaging
.
Using smoothie-gcm
you can:
- Start several
gen_servers
representing differentGCM applications
defined by differentGCM API keys
- Send notification messages to Android mobile devices registered to your specific application and registered to
GCM
using a specificregistration id
So far smoothie-gcm
does only provide support for JSON messages since GCM does not allow to send multicast messages using plain text.
You can use gcm_app as a dependency in your rebar.config:
{deps, [
{smoothie_gcm, ".*", {git, "https://github.com/tapsters/smoothie-gcm.git", {tag, "master"}}}
]}.
While smoothie-gcm
is running you can start several supervised gen_servers, one for each GCM application. Every gen_server is defined by an atom used internally for registration and by a GCM API key
.
1> sm_gcm:start(foo, "myapikey").
{ok,<0.60.0>}
2> sm_gcm:start(bar, "myotherapikey").
{ok,<0.65.0>}
You can stop a gen_server
representing a GCM Application using:
3> sm_gcm:stop(foo).
At any time you can send a GCM message to one or more mobile devices by calling:
4> sm_gcm:push(RegisteredName, RegIds, Message).
or by calling:
5> sm_gcm:sync_push(RegisteredName, RegIds, Message).
Where RegistereName
is the atom used during registration, RegIds
is a list (max 1000 elements) of Registration Ids specified as Erlang binaries (e.g., <<"APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...">>
) and Message
is an Erlang term representing the data you want to send to the device.
The JSON message is built yaws-json2 in the module sm_gcm.erl
and in the end will have the following form:
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
"message" : "a message"
},
"time_to_live" : 3600,
"collapse_key" : "your_update"
}
You can send this message using:
6> sm_gcm:push(default, RegIds1, {struct, [{"data", {struct, [{"message", "a message"}]}},
6> {"time_to_live", 3600},
6> {"collapse_key", "your_update"}]}).
or simply:
7> sm_gcm:push(default, RegIds1, {struct, [{"data", {struct, [{"message", "a message"}]}}]}).
smoothie-gcm
will push the message for you to Google Cloud Messaging
servers and will parse the JSON provided as result.
In order to understand errors see: Interpreting an error response.
Some of the concepts I used for building this Erlang application are based on this blog post
and on this Erlang application for APN
.