Commit 5f3054af authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Issue #39: Add image property to Android

parent 2e7aa899
Loading
Loading
Loading
Loading
+2 −3
Changes for README.md: 2 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -55,9 +55,8 @@ Parameter | Description
`options` | `JSON Object` platform specific initialization options.
`options.android` | `JSON Object` Android specific initialization options.
`options.android.senderID` | `String` Maps to the project number in the Google Developer Console.
`options.android.icon` | `String` Optional. The name of a drawable resource to use as the small-icon. You can also override this option server-side by sending a `icon` key on the gcm data.
`options.android.icon` | `String` Optional. The name of a drawable resource to use as the small-icon.
`options.android.iconColor` | `String` Optional. Sets the background color of the small icon. [Supported Formats](http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String))
`options.android.largeIcon` | `String` Optional. The name of a drawable resource to use as the large-icon. You can also override this option server-side by sending a `largeIcon` key on the gcm data.
`options.ios` | `JSON Object` iOS specific initialization options.
`options.windows` | `JSON Object` Windows specific initialization options.

@@ -105,7 +104,7 @@ Callback Parameter | Description
`data.title` | `String` The optional title of the push message sent from the 3rd party service.
`data.count` | `String` The number of messages to be displayed in the badge iOS or message count in the notification shade in Android. For windows, it represents the value in the badge notification which could be a number or a status glyph.
`data.sound` | `String` The name of the sound file to be played upon receipt of the notification.
`data.image (windows only)` | `String` The path of the image file to be displayed in the notification.
`data.image (android/windows only)` | `String` The path of the image file to be displayed in the notification.
`data.additionalData` | `JSON Object` An optional collection of data sent by the 3rd party push service that does not fit in the above properties.
`data.additionalData.foreground` | `Boolean` Whether the notification was received while the app was in the foreground

+47 −18
Changes for src/android/com/adobe/phonegap/push/GCMIntentService.java: 47 added lines, 18 removed lines.
Original line number Diff line number Diff line
package com.adobe.phonegap.push;

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

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

@@ -10,6 +15,7 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@@ -108,10 +114,8 @@ public class GCMIntentService extends GCMBaseIntentService {
        SharedPreferences prefs = context.getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
        String localIcon = prefs.getString("icon", null);
        String localIconColor = prefs.getString("iconColor", null);
		String localLargeIcon = prefs.getString("largeIcon", null);
        Log.d(LOG_TAG, "stored icon=" + localIcon);
        Log.d(LOG_TAG, "stored iconColor=" + localIconColor);
		Log.d(LOG_TAG, "stored largeIcon=" + localLargeIcon);

        /*
         * Notification Icon Color
@@ -137,8 +141,7 @@ public class GCMIntentService extends GCMBaseIntentService {
         *
         * Sets the small-icon of the notification.
         * 
 		 * - checks the gcm data for the `icon` key
 		 * - if none, checks the plugin options for `icon` key
         * - checks the plugin options for `icon` key
         * - if none, uses the application icon 
         *
         * The icon value must be a string that maps to a drawable resource.
@@ -146,11 +149,7 @@ public class GCMIntentService extends GCMBaseIntentService {
         *
         */
        int iconId = 0;
 		String gcmIcon = extras.getString("icon"); // from gcm
 		if (gcmIcon != null) {
 			iconId = resources.getIdentifier(gcmIcon, "drawable", packageName);
 			Log.d(LOG_TAG, "using icon from gcm");
 		} else if (localIcon != null) {
        if (localIcon != null) {
            iconId = resources.getIdentifier(localIcon, "drawable", packageName);
            Log.d(LOG_TAG, "using icon from plugin options");
        }
@@ -165,23 +164,38 @@ public class GCMIntentService extends GCMBaseIntentService {
         *
         * Sets the large-icon of the notification
         *
 		 * - checks the gcm data for the `largeIcon` key
 		 * - if none, checks the plugin options for `largeIcon` key
         * - checks the gcm data for the `image` key
         * - checks to see if remote image, loads it.
         * - checks to see if assets image, Loads It.
         * - checks to see if resource image, LOADS IT!
         * - if none, we don't set the large icon
         *
         */
 		int largeIconId = 0;
 		String gcmLargeIcon = extras.getString("largeIcon"); // from gcm
        String gcmLargeIcon = extras.getString("image"); // from gcm
        if (gcmLargeIcon != null) {
            if (gcmLargeIcon.startsWith("http://")) {
                mBuilder.setLargeIcon(getBitmapFromURL(gcmLargeIcon));                
                Log.d(LOG_TAG, "using remote large-icon from gcm");    
            } else {
                AssetManager assetManager = getAssets();
                InputStream istr;
                try {
                    istr = assetManager.open(gcmLargeIcon);
                    Bitmap bitmap = BitmapFactory.decodeStream(istr);
                    mBuilder.setLargeIcon(bitmap);
                    Log.d(LOG_TAG, "using assets large-icon from gcm");    
                } catch (IOException e) {
                    int largeIconId = 0;
                    largeIconId = resources.getIdentifier(gcmLargeIcon, "drawable", packageName);
 			Log.d(LOG_TAG, "using large-icon from gcm");	
 		} else if (localLargeIcon != null) {
 			largeIconId = resources.getIdentifier(localLargeIcon, "drawable", packageName);
 			Log.d(LOG_TAG, "using large-icon from plugin options");	
 		}
                    if (largeIconId != 0) {
                        Bitmap largeIconBitmap = BitmapFactory.decodeResource(resources, largeIconId);
                        mBuilder.setLargeIcon(largeIconBitmap);
                        Log.d(LOG_TAG, "using resources large-icon from gcm");    
                    } else {
                        Log.d(LOG_TAG, "Not setting large icon");                        
                    }
                }
            }
        }


@@ -212,6 +226,21 @@ public class GCMIntentService extends GCMBaseIntentService {
        mNotificationManager.notify((String) appName, notId, mBuilder.build());
    }
    
    public 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;
        }
    }
    
    private static String getAppName(Context context) {
        CharSequence appName =  context.getPackageManager().getApplicationLabel(context.getApplicationInfo());
        return (String)appName;
+2 −5
Changes for src/android/com/adobe/phonegap/push/PushPlugin.java: 2 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -84,11 +84,6 @@ public class PushPlugin extends CordovaPlugin {
                } catch (JSONException e) {
                    Log.d(LOG_TAG, "no iconColor option");
                }
                try {
                    editor.putString("largeIcon", jo.getString("largeIcon"));
                } catch (JSONException e) {
                    Log.d(LOG_TAG, "no largeIcon option");
                }
                editor.commit();
            }

@@ -191,6 +186,8 @@ public class PushPlugin extends CordovaPlugin {
					json.put("count", value);
				} else if (key.equals("soundname")) {
					json.put("sound", value);
				} else if (key.equals("image")) {
				    json.put("image", value);
				}
				else if ( value instanceof String ) {
					String strValue = (String)value;