Courtesy: GitHub

Google Cloud Messaging Quickstart

Google Cloud Messaging Android Quickstart app demonstrates registering an Android app for GCM and handling the receipt of a GCM message. InstanceID allows easy registration while GcmReceiver and GcmListenerService provide simple means of receiving and handling messages.

Introduction

Getting Started

  • Follow the quickstart guide to set up your project in Android Studio.
  • Run the sample on your Android device.
  • Update API_KEY in GcmSender.java, with API key from your project.
  • Run the terminal command to send GCM message to your device.
  • A notification containing the GCM message should be displayed on the device.

NOTE

The GcmSender module in this project is emulating a server for the purposes of this sample, but it's not meant to serve as an example for a production app server. For information on GCM server implementaion see About GCM Connection Server

Screenshots

Screenshot

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/google-services/issues

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub.

License

Copyright 2015 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compileSdkVersion 25

applicationId "gcm.play.android.samples.com.gcmquickstart"

minSdkVersion 14

targetSdkVersion 25

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.google.android.gms:play-services-gcm:11.6.0'

compile 'com.android.support:appcompat-v7:25.3.1'

compile 'org.apache.commons:commons-io:1.3.2'

compile 'org.json:json:20140107'

package gcm.play.android.samples.com.gcmquickstart

uses-permission

  • com.google.android.c2dm.permission.RECEIVE
  • android.permission.WAKE_LOCK

MainActivity

Copyright 2015 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Check the device to make sure it has the Google Play Services APK. If it doesn't, display a dialog that allows users to download the APK from the Google Play Store or enable it in the device's system settings.

MyGcmListenerService

Copyright 2015 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Called when message is received. * @param from SenderID of the sender. @param data Data bundle containing message data as key/value pairs. For Set of keys use data.keySet().

Production applications would usually process the message here. Eg: - Syncing with server.

  • Store message in local database.
  • Update UI.

In some cases it may be useful to show a notification indicating to the user that a message was received.

Create and show a simple notification containing the received GCM message. * @param message GCM message received.

MyInstanceIDListenerService

Copyright 2015 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Called if InstanceID token is updated. This may occur if the security of the previous token had been compromised. This call is initiated by the InstanceID provider.

RegistrationIntentService

Copyright 2015 Google Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Persist registration to third-party servers. Modify this method to associate the user's GCM registration token with any server-side account maintained by your application. @param token The new token.

Subscribe to any GCM topics of interest, as defined by the TOPICS constant. * @param token GCM token @throws IOException if unable to reach the GCM PubSub service

GcmReceiver