Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save CHLibrarian/796f09a0243058d30962 to your computer and use it in GitHub Desktop.

Select an option

Save CHLibrarian/796f09a0243058d30962 to your computer and use it in GitHub Desktop.
ContextHub Application Services Receiving Push (Android)
// Implement the PushPayloadHandler interface
public class NotificationHandler implements PushPayloadHandler {
private static final int NOTIFICATION_ID = 1;
private static final String KEY_MESSAGE = "message";
@Override
public void handlePushPayload(Context context, Bundle bundle) {
if(bundle.containsKey(KEY_MESSAGE)) {
showNotification(context, bundle);
}
else {
// background processing of your custom push data
}
}
private void showNotification(Context context, Bundle bundle) {
String message = bundle.get(KEY_MESSAGE).toString();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(context.getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(false)
.setVibrate(new long[]{100, 500, 100, 500})
.build();
manager.notify(NOTIFICATION_ID, notification);
}
}
// Initialize your PushPayloadHandler in the call to Push.init()
public class NotifyMeApp extends Application {
@Override
public void onCreate() {
super.onCreate();
// Register with ContextHub
ContextHub.init(this, "YOUR-APP-ID-HERE");
/* Initialize push service with your GCM sender id, the activity to
launch when a notification is opened, and an instance of the class that implements PushPayloadHandler */
Push.init(this, "YOUR-GCM-PROJECT-ID-HERE", MainActivity.class, new NotificationHandler();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment