Giter Site home page Giter Site logo

gcm-erlang's Introduction

gcm-erlang

Build Status

This software provides an Erlang client for Google Cloud Messaging.

What you can do with gcm-erlang:

With gcm-erlang you can:

  1. start several gen_servers representing different GCM applications defined by different GCM API keys
  2. send notification messages to Android mobile devices registered to your application and registered to GCM with a registration id

So far gcm-erlang provides only support for JSON messages since GCM does not allow to send multicast messages using plain text.

How to compile:

The first thing you have to do is to compile all the Erlang files using rebar.

$ ./rebar get-deps compile

How to use with rebar:

You can use gcm_app as a dependency in your rebar.config:

{deps , [
    {gcm, ".*", {git, "https://github.com/pdincau/gcm-erlang.git", {tag, "1.0.1"}}}
]}.

###How to run tests:

./rebar compile && ./rebar skip_deps=true eunit && ./run-dialyzer.sh

How to run the application gcm-erlang:

Once all the Erlang files are compiled you can start the application gcm-erlang. This application depends on other applications so it is mandatory to start them as well.

$ erl -pa deps/*/ebin -pa ebin
1> application:start(inets).
ok
2> application:start(jsx).
ok
3> ssl:start().
ok
4> application:start(gcm).
ok

How to start/stop different gen_servers under application gcm-erlang (one for each GCM application):

While gcm-erlang 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.

3> gcm:start(foo, "myapikey").
{ok,<0.60.0>}
4> gcm:start(bar, "myotherapikey").
{ok,<0.65.0>}
5> gcm:start(baz, "mylastapikey").
{ok,<0.79.0>}

You can stop a gen_server representing a GCM Application using:

6> gcm:stop(foo).

How to send a GCM message using from a specific GCM application:

At any time you can send a GCM message to one or more mobile devices by calling:

7> gcm:push(RegisteredName, RegIds, Message).

or by calling:

7> 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 using jsx in the module 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:

8> gcm:push(RegisteredName, RegIds, [{<<"data">>, [
8>     {<<"message">>, <<"a message">>}
8> ]}, {<<"time_to_live">>,3600}, {<<"collapse_key">>,<<"your_update">>}]).

or simply:

8> gcm:push(RegisteredName, RegIds, [{<<"data">>, [
8>     {<<"message">>, <<"a message">>}
8> ]}]).

gcm-erlang 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.

Note:

Some of the concepts I used for building this Erlang application are based on this blog post and on this Erlang application for APN.

gcm-erlang's People

Contributors

andekar avatar christian-fei avatar georgeye avatar hairyhum avatar manuel-rubio avatar marcelloceschia avatar pdincau avatar stephandollberg avatar zkessin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gcm-erlang's Issues

jsx make crash the gen_server

Hi,

I have tried GCM, but it doesn't seems to work correctly due to jsx... Here's what I do :

application:start(inets).
application:start(jsx).
application:start(lager).
application:start(gcm).
application:start(crypto).
application:start(public_key).
application:start(ssl).
Callback = fun(Error, RegId) -> io:format("~p pn", [RegId, Error]) end.
gcm:start(android, "MyKey", Callback).
gcm:push(android, ["DeviceToken"], [{<<"data">>, [{<<"message">>, <<"a message">>}]}]).

Of course MyKey and DeviceToken are modified values, working on a php curl script.

Here's the Error I get :

14:24:41.005 [info] Message=[{<<"data">>,[{<<"message">>,<<"a message">>}]}]; RegIds=["DeviceToken"]
ok
11> 14:24:41.398 [error] gen_server android terminated with reason: bad argument in jsx_decoder:value/4 line 221
14:24:41.398 [error] CRASH REPORT Process android with 0 neighbours exited with reason: bad argument in jsx_decoder:value/4 line 221 in gen_server:terminate/6 line 747
14:24:41.399 [error] Supervisor gcm_sup had child gcm started with gcm:start_link(android, "MyKey", #Fun<erl_eval.12.17052888>) at <0.88.0> exit with reason bad argument in jsx_decoder:value/4 line 221 in context child_terminated

Error in request.

Error in request. Reason was: Bad Request - ""registration_ids" field is not a JSON array\n"

when trying to execute: gcm:push(foo,<<"APA91bEoSpQaIob__________S3IgPfk3-ZjwODp4b">>,[{<<"data">>, [{<<"message">>, <<"a message">>}]}]).

unable to understand the significance of registered name

Getting error trying to send push

Getting the error below while trying to send push:

016-03-27 01:21:20.489 [info] <0.914.0> Sending message: [{<<"priority">>,<<"high">>},{<<"delay_while_idle">>,false},{<<"collapse_key">>,<<"1459025480489662">>},{<<"data">>,[{<<"message">>,<<"test">>}]}] to reg ids: [<<"fy7sJxSskqg:APA91bHwPVYlc-he_Xbn8ZqawLn1A22xtZqW_ZnDK4oXjjAXsuqcVG2DHEx-L_2trvaQT1Lyn1HWTlkAkVDJ1PBI2BT2hJ43TFzQkSLBuBs8AqNJu8J4KS-mDDPvt8UAKE7w8Bb7bl3m">>] retries: 3. 2016-03-27 01:21:20.687 [error] <0.1036.0> CRASH REPORT Process <0.1036.0> with 0 neighbours crashed with reason: bad argument in call to erlang:'++'([107,101,121,61|undefined], "\r\n") in http_request:key_value_str/2 line 274 2016-03-27 01:21:20.687 [error] <0.835.0> Supervisor httpc_handler_sup had child undefined started with {httpc_handler,start_link,undefined} at <0.1036.0> exit with reason badarg in context child_terminated

btw, thanks for gcm-erlang :)

Add retry number

Add retry number to function push/3and sync_push/3 so that after N attempts the message will be discarded.

Retry doesn't work

in the "do_backoff" function to make this work

timer:apply_after(RetryAfter * 1000, ?MODULE, do_push, [RegIds, Message, Key, Retry - 1])

i had to export the "do_push" function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.