Created
July 21, 2012 15:56
-
-
Save okiess/3156229 to your computer and use it in GitHub Desktop.
Example implementation of a GCM Intent Service receiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GCMBaseIntentService is part of Google Cloud Messaging which is deprecated
http://stackoverflow.com/questions/11472496/where-is-com-google-android-gcm-gcmbaseintentservice#comment-29077784