Skip to content

GCM Android

Nicos Karalis edited this page Nov 26, 2012 · 2 revisions

Configuration

	GCM.host = 'https://android.googleapis.com/gcm/send'
	# https://android.googleapis.com/gcm/send is default

	GCM.format = :json
	# :json is default and only available at the moment

	GCM.key = "123abc456def"
	# this is the apiKey obtained from here https://code.google.com/apis/console/

Usage

Sending a single notification:

	destination = ["device1", "device2", "device3"]
	# can be an string or an array of strings containing the regIds of the devices you want to send

	data = {:key => "value", :key2 => ["array", "value"]}
	# must be an hash with all values you want inside you notification

	GCM.send_notification( destination )
	# Empty notification

	GCM.send_notification( destination, data )
	# Notification with custom information

	GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false )
	# Notification with custom information and parameters

for more information on parameters check documentation: GCM | Android Developers

Sending multiple notifications:

	destination1 = "device1"
	destination2 = ["device2"]
	destination3 = ["device1", "device2", "device3"]
	# can be an string or an array of strings containing the regIds of the devices you want to send

	data1 = {:key => "value", :key2 => ["array", "value"]}
	# must be an hash with all values you want inside you notification
	
	options1 = {:collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false}
	# options for the notification
	
	n1 = GCM::Notification.new(destination1, data1, options1)
	n2 = GCM::Notification.new(destination2, data2)
	n3 = GCM::Notification.new(destination3, data3, options2)

	GCM.send_notifications( [n1, n2, n3] )
	# In this case, every notification has his own parameters

for more information on parameters check documentation: GCM | Android Developers

Getting your Android device token (regId)

Check this link GCM: Getting Started

(Optional) You can add multiple keys for GCM

You can use multiple keys to send notifications, to do it just do this changes in the code

Configure

	GCM.key = { :key1 => "123abc456def", :key2 => "456def123abc" }
	# the ``:key1`` and the ``:key2`` can be any object, they can be the projectID, the date, the version, doesn't matter.
	# The only restrain is: they need to be valid keys for a hash.

Usage

	# For single notification
	GCM.send_notification( destination, :identity => :key1 )
	# Empty notification
	
	GCM.send_notification( destination, data, :identity => :key1 )
	# Notification with custom information
	
	GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false, :identity => :key1 )
	# Notification with custom information and parameters
	
	# For multiple notifications
	options1 = {}
	options2 = {..., :identity => :key2}
	n1 = GCM::Notification.new(destination1, data1, options1.merge({:identity => :key2}))
	n2 = GCM::Notification.new(destination2, data2, :identity => :key1)
	n3 = GCM::Notification.new(destination3, data3, options2)
	
	GCM.send_notifications( [n1, n2, n3] )
	# In this case, every notification has his own parameters, options and key