Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example implementation of a GCM Intent Service receiver
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;
import com.apphoshies.core.services.DeviceToken;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static int count = 0;
/**
* @see com.google.android.gcm.GCMBaseIntentService#onError(android.content.Context, java.lang.String)
*/
@Override
protected void onError(Context arg0, String error) {
Log.d("ApphoshiesFramework", "onError: " + error);
}
/**
* @see com.google.android.gcm.GCMBaseIntentService#onRegistered(android.content.Context, java.lang.String)
*/
@Override
protected void onRegistered(Context context, String regId) {
Log.d("ApphoshiesFramework", "onRegistered: " + regId);
DeviceToken.registerDeviceForGCM(context, regId);
}
/**
* @see com.google.android.gcm.GCMBaseIntentService#onUnregistered(android.content.Context, java.lang.String)
*/
@Override
protected void onUnregistered(Context context, String regId) {
Log.d("ApphoshiesFramework", "onUnregistered: " + regId);
DeviceToken.unregisterDeviceFromGCM(context, regId);
}
/**
* @see com.google.android.gcm.GCMBaseIntentService#onMessage(android.content.Context, android.content.Intent)
*/
@Override
protected void onMessage(Context context, Intent intent) {
Log.d("ApphoshiesFramework", "onMessage");
// Please customize!
// This is how to get values from the push message (data)
String message = intent.getStringExtra("message");
long timestamp = intent.getLongExtra("timestamp", -1);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.dialog, "App Notification", System.currentTimeMillis());
Intent notificationIntent = new Intent(context, Main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
note.setLatestEventInfo(context, "App Notification", message, pendingIntent);
note.number = count++;
note.defaults |= Notification.DEFAULT_SOUND;
note.defaults |= Notification.DEFAULT_VIBRATE;
note.defaults |= Notification.DEFAULT_LIGHTS;
note.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, note);
}
}
@younes0

This comment has been minimized.

Copy link

commented Mar 4, 2014

GCMBaseIntentService is part of Google Cloud Messaging which is deprecated
http://stackoverflow.com/questions/11472496/where-is-com-google-android-gcm-gcmbaseintentservice#comment-29077784

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.