package com.octgn.app;

import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GcmIntentService extends IntentService {
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    private String TAG = "OCTGN";
    NotificationCompat.Builder builder;

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
                // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Received: " + extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_drawer)
                        .setContentTitle("GCM Notification")
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(msg))
                        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

@@ -392,16 +392,79 @@
 ing;%0D%0A%0D%0A
+import org.json.JSONException;%0D%0Aimport org.json.JSONObject;%0D%0A%0D%0A
 public c
@@ -1639,33 +1639,26 @@
         
-sendNotification(
+Log.e(TAG,
 %22Send er
@@ -1731,38 +1731,16 @@
 ssaging.
-%0D%0A                    
 MESSAGE_
@@ -1792,33 +1792,26 @@
         
-sendNotification(
+Log.e(TAG,
 %22Deleted
@@ -1838,32 +1838,8 @@
  %22 +
-%0D%0A                      
   ex
@@ -1966,38 +1966,16 @@
 ssaging.
-%0D%0A                    
 MESSAGE_
@@ -2031,609 +2031,51 @@
     
-// This loop represents the service doing some work.%0D%0A                for (int i=0; i%3C5; i++) %7B%0D%0A                    Log.i(TAG, %22Working... %22 + (i + 1)%0D%0A                            + %22/5 @ %22 + SystemClock.elapsedRealtime());%0D%0A                    try %7B%0D%0A                        Thread.sleep(5000);%0D%0A                    %7D catch (InterruptedException e) %7B%0D%0A                    %7D%0D%0A                %7D%0D%0A                Log.i(TAG, %22Completed work @ %22 + SystemClock.elapsedRealtime());%0D%0A                // Post notification of received message.%0D%0A                sendNotification(%22Received: %22 + extras.toString(
+sendNotification(extras.getString(%22message%22
 ));%0D
@@ -2511,24 +2511,43 @@
 ing msg) %7B%0D%0A
+        try %7B%0D%0A    
         mNot
@@ -2604,16 +2604,20 @@
         
+ 
+   
 this.get
@@ -2660,24 +2660,28 @@
 ERVICE);%0D%0A%0D%0A
+    
         Pend
@@ -2754,24 +2754,28 @@
             
+   
+ 
 new Intent(t
@@ -2803,24 +2803,147 @@
 ss), 0);%0D%0A%0D%0A
+            JSONObject jObject = new JSONObject(msg);%0D%0A%0D%0A            String message = jObject.getString(%22Message%22);%0D%0A%0D%0A    
         Noti
@@ -2989,24 +2989,28 @@
             
+   
+ 
 new Notifica
@@ -3055,24 +3055,28 @@
             
+    
 .setSmallIco
@@ -3094,15 +3094,10 @@
 e.ic
-_drawer
+on
 )%0D%0A 
@@ -3111,32 +3111,36 @@
                 
+  
+  
 .setContentTitle
@@ -3145,28 +3145,21 @@
 le(%22
-GCM Notification%22)%0D%0A
+OCTGN%22)%0D%0A    
     
@@ -3259,16 +3259,20 @@
         
+    
 .bigText
@@ -3273,18 +3273,22 @@
 igText(m
-sg
+essage
 ))%0D%0A    
@@ -3299,32 +3299,36 @@
                 
+    
 .setContentText(
@@ -3328,24 +3328,32 @@
 ntText(m
-sg
+essage
 );%0D%0A%0D%0A
+    
         
@@ -3391,24 +3391,28 @@
 ntIntent);%0D%0A
+    
         mNot
@@ -3468,22 +3468,133 @@
 .build());%0D%0A
+        %7D catch (JSONException e) %7B%0D%0A            Log.i(TAG,msg);%0D%0A            e.printStackTrace();%0D%0A        %7D%0D%0A
     %7D%0D%0A%7D%0D%0A

Properly parse the gcm message. Hells yeah