Skip to content

An Android library that makes Google Cloud Messaging simple.

Notifications You must be signed in to change notification settings

Rgghgh/GCMLibrary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Google Cloud Messaging Library

A library that makes Google Cloud Messaging between Android Devices simple.

see Google Cloud Messaging.

How to install:

  1. Copy gcm package into your project and add this config to the 'app' directory.

  2. Permissions: (Notice where you should insert you package name)

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<permission android:name="<YOUR PACKAGE NAME>.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<uses-permission android:name="<YOUR PACKAGE NAME>.permission.C2D_MESSAGE"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
  1. Registrations (in manifest file):
<service android:name=".gcm.GcmRegistrationService"/>
<service android:name=".gcm.GcmMessageListener" android:exported="false">
	<intent-filter>
		<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
	</intent-filter>
</service>
<receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
  <intent-filter>
  	<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
  
  	<category android:name="<YOUR PACKAGE NAME HERE>"/>
  </intent-filter>
</receiver>

And you should be good to go.

How to use:

  1. Implement the "GcmHandler" callback class and its methods. Can be implemented by an Activity.
public class MainActivity implements GcmHandler
  1. Create a "Gcm" instance:
Gcm gcm = new Gcm(context, gcmHandler);

2.1) Make sure to set your API key: (the one you got from "How to install: #1)

gcm.setApiKey(myApiKey);

This is important to do right after creating an instance.

  1. To obtain the unique key of this device, needed to send messages to it, use:
gcm.requestDeviceToken();

The token will we requested and returned to the GcmHandler's onGcmTokenReceived() method. The token should behave like a "Phone number" and generaly does not change.

  1. Now you can send Messages!
gcm.sendMessage(toDeviceToken, bundledMessage)

When the message is sent, the GcmHandler's onGcmMessageSent() is called. Messages you receive will go to the GcmHandler's onGcmMessageReceived().

About

An Android library that makes Google Cloud Messaging simple.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages