Simple GCM Test project with SNS integration.
-
In Android Studio create a new project with application name GCMSampleApp, Company domain example.com and select Empty Activity.
-
Enable Google services for this application by visiting here.
- For app name enter GCMSampleApp.
- For package name enter com.example.gcmsampleapp.
- Click Choose and configure services.
- Click Cloud Messaging and Enable.
- Click Generate configuration files.
- Click Download google-services.json.
- Note down Server API Key as we will need this to push notification both using curl and SNS.
-
Move the downloaded google-services.json file to GCMSampleApp project app/ directory.
-
In project level build.gradle under dependencies {..} include the following classpath:
classpath 'com.google.gms:google-services:3.0.0'
-
In application level build.gradle add the following under dependencies {..}:
compile 'com.google.android.gms:play-services-gcm:9.6.1'
-
Also, in application level build.gradle add the following at the end of the file:
apply plugin: 'com.google.gms.google-services'
-
Add a new Java class RegistrationIntentService under com.example.gcmsampleapp package with the following contents:
package com.example.gcmsampleapp; import android.app.IntentService; import android.content.Intent; import android.util.Log; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegIntentService"; public RegistrationIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.i(TAG, "Getting token."); try { InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); Log.i(TAG, "GCM Registration Token: " + token); // TODO: Implement this method to send any registration to your app's servers. sendRegistrationToServer(token); } catch (Exception e) { Log.d(TAG, "Failed to complete token refresh", e); } } private void sendRegistrationToServer(String token) { } }
-
Add a new Java class MyGcmListenerService under com.example.gcmsampleapp package with the following contents:
package com.example.gcmsampleapp; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.util.Log; import com.google.android.gms.gcm.GcmListenerService; public class MyGcmListenerService extends GcmListenerService { private static final String TAG = "MyGcmListenerService"; @Override public void onMessageReceived(String from, Bundle data) { String title = data.getString("title"); String message = data.getString("message"); Log.d(TAG, "From: " + from); Log.d(TAG, "Title: " + title); Log.d(TAG, "Message: " + message); sendNotification(title, message); } private void sendNotification(String title, String message) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.common_ic_googleplayservices) .setContentTitle(title) .setContentText(message) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); } }
-
Match or replace the content of MainActivity class with the following:
package com.example.gcmsampleapp; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GoogleApiAvailability; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (checkPlayServices()) { // Start IntentService to register this application with GCM. Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } private boolean checkPlayServices() { GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance(); int resultCode = apiAvailability.isGooglePlayServicesAvailable(this); if (resultCode != ConnectionResult.SUCCESS) { if (apiAvailability.isUserResolvableError(resultCode)) { apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST) .show(); } else { Log.i(TAG, "This device is not supported."); finish(); } return false; } return true; } }
-
Match or replace AndroidManifest.xml file with the following:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gcmsampleapp"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <permission android:name="com.example.gcmsampleapp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.gcmsampleapp.permission.C2D_MESSAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <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="com.example.gcmsampleapp" /> </intent-filter> </receiver> <service android:name="com.example.gcmsampleapp.MyGcmListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <service android:name="com.example.gcmsampleapp.RegistrationIntentService" android:exported="false"> </service> </application> </manifest>
-
Run project to get Registration Token which will be printed out in the console.
-
Create a file notification.json with the following content. Replace
"to":
value with your device Registration Token:{ "to": "<DEVICE REGISTRATION TOKEN HERE>", "data": { "message":"Test message from curl.", "title":"GCMSampleApp" } }
-
Run the following CURL command to publish directly to GCM. Replace
<API KEY HERE>
with your Server API Key:curl https://gcm-http.googleapis.com/gcm/send -H "Authorization: key=<API KEY HERE>" -H "Content-Type: application/json" -d @notification.json
Advantage of using SNS is scalability which is required when publishing tens of millions of notifications in a very short time and abstracts interaction with different push services behind a unified API.
-
Add a new platform application in SNS console -> Applications.
-
Enter GCMSampleApp for the name.
-
Select GCM from the Push Notification Platform drop down menu.
-
In the API key field paste your Server API Key.
-
Click Create Platform Application button.
-
Click on the new application ARN to enter.
-
Click Create Platform Endpoint button.
-
Paste your Device Token in the Device token field.
-
Enter optional data in User Data field.
-
Click Add Endpoint button.
-
Select the newly added endpoint from the list.
-
Click Publish to endpoint button.
-
Select JSON for Message format.
-
Enter the following in the Message box and click Publish message button.
{ "GCM": "{ \"data\": { \"message\": \"Test message from SNS console.\", \"title\": \"GCMSampleApp\" } }" }