Coder Social home page Coder Social logo

gcm's Introduction

gcm

The Android SDK provides a nice convenience library (com.google.android.gcm.server) that greatly simplifies the interaction between Java-based application servers and Google's GCM servers. However, Google has not provided much support for application servers implemented in languages other than Java, specifically those written in the Go programming language. The gcm package helps to fill in this gap, providing a simple interface for sending GCM messages and automatically retrying requests in case of service unavailability.

Documentation: http://godoc.org/github.com/alexjlockwood/gcm

Getting Started

To install gcm, use go get:

go get github.com/alexjlockwood/gcm

Import gcm with the following:

import "github.com/alexjlockwood/gcm"

Sample Usage

Here is a quick sample illustrating how to send a message to the GCM server:

package sample

import (
    "fmt"
    "net/http"
    "github.com/alexjlockwood/gcm"
)

func main() {
    // Create the message to be sent
    regIds := []string{"4","8","15","16","23","42"}
    data := map[string]string{"score": "5x1", "time": "15:10"}
    msg := gcm.NewMessage(data, regIds...)

    // Create a Sender to send the message
    sender := &gcm.NewSender("sample_api_key")
    
    // Send the message and receive the response after at most two retries.
    response, err := sender.Send(msg, 2)
    if err != nil {
        fmt.Println("Failed to send message: " + err.Error())
        return       
    }
}

Note for Google AppEngine users

If your application server runs on Google AppEngine, you must import the appengine/urlfetch package and create the Sender as follows:

import (
    "appengine"
    "appengine/urlfetch"
    "github.com/alexjlockwood/gcm"
)

func handler(w http.ResponseWriter, r *http.Request) {
    /* ... */

    c := appengine.NewContext(r)
    client := urlfetch.Client(c)
    sender := gcm.NewSender("sample_api_key", client)

    /* ... */
}        

gcm's People

Contributors

alexjlockwood avatar

Watchers

Chemicalweb avatar James Cloos avatar

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.