/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.beepscore.android.sunshine.gcm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

import com.beepscore.android.sunshine.MainActivity;
import com.beepscore.android.sunshine.R;
import com.google.android.gms.gcm.GcmListenerService;

import org.json.JSONException;
import org.json.JSONObject;

public class MyGcmListenerService extends GcmListenerService {

    private static final String TAG = "MyGcmListenerService";

    private static final String EXTRA_DATA = "data";
    private static final String EXTRA_WEATHER = "weather";
    private static final String EXTRA_LOCATION = "location";

    public static final int NOTIFICATION_ID = 1;

    /**
     * Called when message is received.
     *
     * @param from SenderID of the sender.
     * @param data Data bundle containing message data as key/value pairs.
     *             For Set of keys use data.keySet().
     */
    @Override
    public void onMessageReceived(String from, Bundle data) {
        // Time to unparcel the bundle!
        if (!data.isEmpty()) {
            // TODO: gcm_default sender ID comes from the API console
            String senderId = getString(R.string.gcm_defaultSenderId);
            if (senderId.length() == 0) {
                Toast.makeText(this, "SenderID string needs to be set", Toast.LENGTH_LONG).show();
            }
            // Not a bad idea to check that the message is coming from your server.
            if ((senderId).equals(from)) {
                // Process message and then post a notification of the received message.
                try {
                    JSONObject jsonObject = new JSONObject(data.getString(EXTRA_DATA));
                    String weather = jsonObject.getString(EXTRA_WEATHER);
                    String location = jsonObject.getString(EXTRA_LOCATION);
                    String alert =
                            String.format(getString(R.string.gcm_weather_alert), weather, location);
                    sendNotification(alert);
                } catch (JSONException e) {
                    // JSON parsing failed, so we just let this message go, since GCM is not one
                    // of our critical features.
                }
            }
            Log.i(TAG, "Received: " + data.toString());
        }
    }

    /**
     *  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.
     *
     * @param message The alert message to be posted.
     */
    private void sendNotification(String message) {
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent =
                PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        // Notifications using both a large and a small icon (which yours should!) need the large
        // icon as a bitmap.
        // Create largeIcon from the resource ID
        Bitmap largeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.art_storm);

        // Generally, you want to set the small icon to the app icon,
        // so that users understand what app is triggering this notification.

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.art_clear)
                        .setLargeIcon(largeIcon)
                        .setContentTitle("Weather Alert!")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                        .setContentText(message)
                        .setPriority(NotificationCompat.PRIORITY_HIGH);
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}

@@ -1150,68 +1150,8 @@
 e;%0A%0A
-import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 publ
@@ -1823,17 +1823,9 @@
  // 
-Time to u
+U
 npar
@@ -1838,17 +1838,16 @@
 e bundle
-!
 %0A       
@@ -1874,78 +1874,8 @@
 ) %7B%0A
-            // TODO: gcm_default sender ID comes from the API console%0A
     
@@ -2100,92 +2100,8 @@
   %7D%0A
-            // Not a bad idea to check that the message is coming from your server.%0A
     
@@ -2162,14 +2162,11 @@
  // 
-Proces
+thi
 s me
@@ -2175,175 +2175,53 @@
 age 
-and then post a notification of the received message.%0A                try %7B%0A                    JSONObject jsonObject = new JSONObject(data.getString(EXTRA_DATA));%0A   
+came from our server, safe to respond to it.%0A
     
@@ -2228,25 +2228,24 @@
             
- 
 String weath
@@ -2249,26 +2249,20 @@
 ather = 
-jsonObject
+data
 .getStri
@@ -2292,28 +2292,24 @@
             
-   
- 
 String locat
@@ -2318,18 +2318,12 @@
 n = 
-jsonObject
+data
 .get
@@ -2358,28 +2358,24 @@
             
-   
- 
 String alert
@@ -2373,28 +2373,24 @@
 ing alert =%0A
-    
             
@@ -2490,20 +2490,16 @@
         
-    
 sendNoti
@@ -2519,216 +2519,8 @@
 t);%0A
-                %7D catch (JSONException e) %7B%0A                    // JSON parsing failed, so we just let this message go, since GCM is not one%0A                    // of our critical features.%0A                %7D%0A
     
@@ -2665,16 +2665,39 @@
  post it
+ on this Android device
 .%0A     *

Revise MyGcmListenerServices onMessageReceived as described in pull request. This fixes a crash. Revise comments.