Giter Site home page Giter Site logo

gcm's Introduction

Google Cloud Messaging is now deprecated.

Google Cloud Messaging has been deprecated in favor of Firebase Cloud Messaging. Please use the fcm gem moving forward.

As of April 10, 2018, Google has deprecated GCM. The GCM server and client APIs are deprecated and will be removed as soon as April 11, 2019. Migrate GCM apps to Firebase Cloud Messaging (FCM), which inherits the reliable and scalable GCM infrastructure, plus many new features. See the migration guide to learn more.


Google Cloud Messaging (GCM) for Android and iOS

Gem Version Build Status

The GCM gem lets your ruby backend send notifications to Android and iOS devices via Google Cloud Messaging.

Installation

$ gem install gcm

or in your Gemfile just include it:

gem 'gcm'

Requirements

An Android device running 2.3 (or newer) or an iOS device and an API key as per GCM getting started guide.

One of the following, tested Ruby versions:

  • 2.0.0
  • 2.1.8
  • 2.2.4
  • 2.3.0

Usage

For your server to send a message to one or more devices, you must first initialise a new GCM class with your Api key, and then call the send method on this and give it 1 or more (up to 1000) registration tokens as an array of strings. You can also optionally send further HTTP message parameters like data or time_to_live etc. as a hash via the second optional argument to send.

Example sending notifications:

require 'gcm'

gcm = GCM.new("my_api_key")
# you can set option parameters in here
#  - all options are pass to HTTParty method arguments
#  - ref: https://github.com/jnunemaker/httparty/blob/master/lib/httparty.rb#L29-L60
#  gcm = GCM.new("my_api_key", timeout: 3)

registration_ids= ["12", "13"] # an array of one or more client registration tokens
options = {data: {score: "123"}, collapse_key: "updated_score"}
response = gcm.send(registration_ids, options)

Currently response is just a hash containing the response body, headers and status. Check here to see how to interpret the responses.

Device Group Messaging

With device group messaging, you can send a single message to multiple instance of an app running on devices belonging to a group. Typically, "group" refers a set of different devices that belong to a single user. However, a group could also represent a set of devices where the app instance functions in a highly correlated manner. To use this feature, you will first need an initialised GCM class.

Generate a Notification Key for device group

Then you will need a notification key which you can create for a particular key_name which needs to be uniquely named per app in case you have multiple apps for the same project_id. This ensures that notifications only go to the intended target app. The create method will do this and return the token notification_key, that represents the device group, in the response:

response = gcm.create(key_name: "appUser-Chris",
                project_id: "my_project_id", # https://developers.google.com/cloud-messaging/gcm#senderid
                registration_ids:["4", "8", "15", "16", "23", "42"])

Send to Notification Key

Now you can send a message to a particular notification_key via the send_with_notification_key method. This allows the server to send a single data to multiple app instances (typically on multiple devices) owned by a single user (instead of sending to some registration tokens). Note: the maximum number of members allowed for a notification_key is 20.

response = gcm.send_with_notification_key("notification_key", {
            data: {score: "3x1"},
            collapse_key: "updated_score"})

Add/Remove Registration Tokens

You can also add/remove registration Tokens to/from a particular notification_key of some project_id. For example:

response = gcm.add(key_name: "appUser-Chris",
                project_id: "my_project_id",
                notification_key:"appUser-Chris-key",
                registration_ids:["7", "3"])

response = gcm.remove(key_name: "appUser-Chris",
                project_id: "my_project_id",
                notification_key:"appUser-Chris-key",
                registration_ids:["8", "15"])

Send Messages to Topics

GCM topic messaging allows your app server to send a message to multiple devices that have opted in to a particular topic. Based on the publish/subscribe model, topic messaging supports unlimited subscriptions per app. Sending to a topic is very similar to sending to an individual device or to a user group, in the sense that you can use the gcm.send_with_notification_key() method where the noticiation_key matches the regular expression "/topics/[a-zA-Z0-9-_.~%]+":

response = gcm.send_with_notification_key("/topics/yourTopic", {
            data: {message: "This is a GCM Topic Message!"})

Or you can use the helper:

response = gcm.send_to_topic("yourTopic", {
            data: {message: "This is a GCM Topic Message!"})

Blog Posts

Mobile Clients

You can find a guide to implement an Android Client app to receive notifications here: Set up a GCM Client App on Android.

The guide to set up an iOS app to get notifications is here: Setting up a GCM Client App on iOS.

ChangeLog

0.1.1

  • Added helper send_to_topic to send messages to topics.
  • Fixed documentation and updated base uri to https://gcm-http.googleapis.com/gcm

0.1.0

  • Added send_with_notification_key to send message to a notification key since the documented API for it is wrong.

0.0.9

  • Check for NotRegistered error and return unregistered ids if this occurs

0.0.8

  • Added support for User Notifications API
  • Added alias method send for send_notification

0.0.7

  • All responses now have a body and header hashes

0.0.6

0.0.5

##MIT License

  • Copyright (c) 2016 Kashif Rasul and Shoaib Burq. See LICENSE.txt for details.

##Many thanks to all the contributors

Donations

We accept tips through Gratipay.

Gratipay

gcm's People

Contributors

bill-kolokithas avatar ferdinandrosario avatar joemasilotti avatar kashif avatar kenleytomlin avatar mbillard avatar nevalla avatar sabman avatar seuros avatar shamithc 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gcm's Issues

google io 2014

Add support for:

  • Cloud Connection Server
  • Upstream messaging
  • User Notifications

How to determine which are invalid / expired registrations ids?

How to determine which are invalid / expired registrations ids from the response?

Am I missing something:

    gcm = GCM.new(ENV['GCM_API_KEY'])
    options = { data: { title: 'test title', message: 'test message', :'user-id' => 1}, collapse_key: SecureRandom.uuid }
    gcm.send(['a', 'b'], options)

I am deliberately sending invalid registration ids.

I get:

{
                  :body => "{\"multicast_id\":9121580178438514214,\"success\":0,\"failure\":2,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"},{\"error\":\"InvalidRegistration\"}]}",
               :headers => {
                  "content-type" => [
            [0] "application/json; charset=UTF-8"
        ],
                          "date" => [
            [0] "Wed, 06 Jan 2016 13:32:00 GMT"
        ],
                       "expires" => [
            [0] "Wed, 06 Jan 2016 13:32:00 GMT"
        ],
                 "cache-control" => [
            [0] "private, max-age=0"
        ],
        "x-content-type-options" => [
            [0] "nosniff"
        ],
               "x-frame-options" => [
            [0] "SAMEORIGIN"
        ],
              "x-xss-protection" => [
            [0] "1; mode=block"
        ],
                        "server" => [
            [0] "GSE"
        ],
            "alternate-protocol" => [
            [0] "443:quic,p=1"
        ],
                       "alt-svc" => [
            [0] "quic=\":443\"; ma=604800; v=\"30,29,28,27,26,25\""
        ],
                 "accept-ranges" => [
            [0] "none"
        ],
                          "vary" => [
            [0] "Accept-Encoding"
        ],
                    "connection" => [
            [0] "close"
        ]
    },
           :status_code => 200,
              :response => "success",
         :canonical_ids => [],
    :not_registered_ids => []
}

not_registered_ids is empty. How would I know which were invalid / expired registration ids?

Sending silent push notifications

Hi, sorry if this question is stupid, but I was wondering whether there's a way to send silent push notifications and how to do so.

Also, is there a place to read documentation on how to use this gem? I'm guessing my question is simple, but couldn't find it in the readme. Thanks!

Response

I am getting this message in my response
"Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields."
Can you please help me out my code is
GCM_CONNECTION.send(gcm_id, {sucess: true})

Can't open my rails console after adding 'gcm' to Gemfile

/home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/gcm-0.0.9/lib/gcm.rb:5:in `<top (required)>': GCM is not a class (TypeError)
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:76:in `require'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:72:in `each'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:72:in `block in require'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:61:in `each'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler/runtime.rb:61:in `require'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1@global/gems/bundler-1.7.3/lib/bundler.rb:133:in `require'
    from /home/shivakumaarmgs/Rails/PCA/config/application.rb:15:in `<top (required)>'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:146:in `require'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:146:in `require_application_and_environment!'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/shivakumaarmgs/.rvm/gems/ruby-2.1.1/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

Stop support of GCM

Hello,

I have heard that Google will stop support of GCM in near future.
Is this true ?
I have already used GCM in my 2-3 Projects.And Projects are in Ruby version 1.9.x
And FCM which is newer Version of GCM only supports version 2.0.x or higher...

So Is there any workaround of my problem?

Setting up tests?

Is it possible to actually test (rspec) if a push notification was sent?

Sending images/ bidirectional messages.

I've used this gem for sending simple messages to single and multiple android devices and it worked like a charm. Great Work guys!
I was wondering though, what do I do for sending upstream messages, from device to app-servers?
Also, how to send rich notifications? Mostly images are what I have in mind.

Send to all?

I just read the README and am wondering whether there's a way to send a message to every device, without listing any registration_ids explicitly?

Push Delivery is so slow 100 Push Takes 45 Seconds

Hello, i have tested for several times and it all takes around 40 seconds just for 100 push notification.

I'm able to send 150.000 push notification in under 60 second in ios so i dont think thats acceptable for 100 notification to deliver in 40 seconds.

my code

start time 2015-05-05 19:15:36 +0300

pusher=GCM.new(application.google_api_key)

2.1.2 :010 > for i in 0..100
2.1.2 :011?> puts i
2.1.2 :012?> options={data: {message:"#{i}. iteration",title:"test"}}
2.1.2 :013?> register_id=["device-token"]
2.1.2 :014?> pusher.send(register_id,options)
2.1.2 :015?> end

i noticed that it only go to the next payload when the previos one is already sent.is that the problem?

Problem with iOS notifications when the app is in the background

I am using this gem to set up my iOS app receive notifications from GCM. My app does indeed receive notifications when its active(on the foreground), however when its not active, it does not receive anything unless I make it active again. From what I read through multiple sources, I must send a this key value pair within my JSON post request: "content_available" : "true" but when I do so I receive an internal server error although I have the APNs certificate. Any thought on why this gem is not working when my app is on background and its indeed working when its active?

Handling for all response codes

As of now, if the response from send_notification is anything other than 200, 400, 401, 500 or 503, the build_response method will return nil instead of a hash.

Would it be possible to have catch all case that would contain as much information about the response as possible.

Or at least an exception with any data that might help in debugging the issue.

Thanks

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.