package com.plugin.gcm;

import java.util.List;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;

import com.google.android.gcm.GCMBaseIntentService;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.graphics.Color;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

@SuppressLint("NewApi")
public class GCMIntentService extends GCMBaseIntentService {

  public static final int NOTIFICATION_ID = 237;
  private static final String TAG = "GCMIntentService";
  
  public GCMIntentService() {
    super("GCMIntentService");
  }

  @Override
  public void onRegistered(Context context, String regId) {

    Log.v(TAG, "onRegistered: "+ regId);

    JSONObject json;

    try {
      json = new JSONObject();
      json.put("event", "registered");
      json.put("regid", regId);

      Log.v(TAG, "onRegistered: " + json.toString());

      // Send this JSON data to the JavaScript application above EVENT should be set to the msg type
      // In this case this is the registration ID
      PushPlugin.sendJavascript(json);

    } catch( JSONException e) {
      // No message to the user is sent, JSON failed
      Log.e(TAG, "onRegistered: JSON exception");
    }
  }

  @Override
  public void onUnregistered(Context context, String regId) {
    Log.d(TAG, "onUnregistered - regId: " + regId);
  }

  @Override
  protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);

    Bundle extras = intent.getExtras();
    
    String silent = extras.getString("silent");
    
    if (silent != null && silent.equals("true")) {
      onSilentMessage(context, extras);
    } else {
      onLoudMessage(context, extras);
    }
  }

  protected void onSilentMessage(Context context, Bundle extras) {
    try {
      JSONObject json = new JSONObject();
      
      String payload = extras.getString("payload");
      
      JSONObject jsonPayload = new JSONObject(payload);
      
      json.put("event", "silentpush");
      json.put("foreground", PushPlugin.isInForeground());
      json.put("payload", jsonPayload);
      
      Log.v(TAG, "onMessage: " + json.toString());

      // Send this JSON data to the JavaScript application above EVENT should be set to the msg type
      // In this case this is the registration ID
      PushPlugin.sendJavascript(json);

    } catch(JSONException e) {
      // No message to the user is sent, JSON failed
      Log.e(TAG, "onSilentMessage: JSON exception");
    }
  }

  protected void onLoudMessage(Context context, Bundle extras) {
    createNotification(context, extras);
  }

  public void createNotification(Context context, Bundle extras) {
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String appName = getAppName(this);

    Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    notificationIntent.putExtra("pushBundle", extras);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    String payload = extras.getString("payload");

    try {
      JSONObject json = new JSONObject(payload);
      
      NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(context)
          .setAutoCancel(true)
          .setContentTitle(json.getString("title"))
          .setContentText(json.getString("message"))
          .setTicker(json.getString("message"))
          .setSmallIcon(context.getApplicationInfo().icon)
          .setVibrate(new long[] { 300, 300, 300, 300, 300 })
          .setContentIntent(contentIntent)
          .setWhen(System.currentTimeMillis());

      String hexColor = json.getString("led_color");
      if (hexColor != null) {
        hexColor = hexColor.replace("#", "");
        int aRGB = Integer.parseInt(hexColor, 16);
        aRGB += 0xFF000000;
        mBuilder.setLights(aRGB, 3000, 3000);
      }

      String iconUrl = json.getString("icon_url");
      if (iconUrl != null) {
        mBuilder.setLargeIcon(getBitmapFromURL(iconUrl));
      }

      mNotificationManager.notify(json.getInt("notification_id"), mBuilder.build());
    } catch(JSONException e) {
      // No message to the user is sent, JSON failed
      Log.e(TAG, "createNotification: JSON exception", e);
    }
  }

  public static void cancelNotification(Context context)
  {
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.cancel((String)getAppName(context), NOTIFICATION_ID);
  }

  private static String getAppName(Context context)
  {
    CharSequence appName =
        context
          .getPackageManager()
          .getApplicationLabel(context.getApplicationInfo());

    return (String)appName;
  }

  public static Bitmap getBitmapFromURL(String strURL) {
      try {
          URL url = new URL(strURL);
          HttpURLConnection connection = (HttpURLConnection) url.openConnection();
          connection.setDoInput(true);
          connection.connect();
          InputStream input = connection.getInputStream();
          Bitmap myBitmap = BitmapFactory.decodeStream(input);
          return myBitmap;
      } catch (IOException e) {
          e.printStackTrace();
          return null;
      }
  }
  
  @Override
  public void onError(Context context, String errorId) {
    Log.e(TAG, "onError - errorId: " + errorId);
  }
}

@@ -3701,24 +3701,72 @@
 payload%22);%0A%0A
+    if (payload == null) %7B%0A      return;%0A    %7D%0A%0A
     try %7B%0A  

Update GCMIntentService.java