Commit 2e7aa899 authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Merge branch 'fix-android-icons' of https://github.com/Deminetix/phonegap-plugin-push into icons

parents 0a078e56 f9b82fe5
Loading
Loading
Loading
Loading
+3 −0
Changes for README.md: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -55,6 +55,9 @@ 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.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.

+88 −1
Changes for src/android/com/adobe/phonegap/push/GCMIntentService.java: 88 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -9,6 +9,11 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
@@ -73,6 +78,8 @@ public class GCMIntentService extends GCMBaseIntentService {
	public void createNotification(Context context, Bundle extras) {
		NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		String appName = getAppName(this);
		String packageName = context.getPackageName();
		Resources resources = context.getResources();

		Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
		notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
@@ -91,13 +98,93 @@ public class GCMIntentService extends GCMBaseIntentService {
		NotificationCompat.Builder mBuilder =
			new NotificationCompat.Builder(context)
				.setDefaults(defaults)
				.setSmallIcon(context.getApplicationInfo().icon)
				.setWhen(System.currentTimeMillis())
				.setContentTitle(extras.getString("title"))
				.setTicker(extras.getString("title"))
				.setContentIntent(contentIntent)
				.setAutoCancel(true);

		
 		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
 		 *
 		 * Sets the small-icon background color of the notification.
 		 * To use, add the `iconColor` key to plugin android options
 		 *
 		 */
		int iconColor = 0;
		if (localIconColor != null) {
			try {
				iconColor = Color.parseColor(localIconColor);
			} catch (IllegalArgumentException e) {
				Log.e(LOG_TAG, "couldnt parse color from android options");
			}
		}
		if (iconColor != 0) {
			mBuilder.setColor(iconColor);
		}

 		/*
 		 * Notification Icon
 		 *
 		 * Sets the small-icon of the notification.
 		 * 
 		 * - checks the gcm data for the `icon` key
 		 * - if none, 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.
 		 * If no resource is found, falls
 		 *
 		 */
 		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) {
 			iconId = resources.getIdentifier(localIcon, "drawable", packageName);
 			Log.d(LOG_TAG, "using icon from plugin options");
 		}
 		if (iconId == 0) {
 			Log.d(LOG_TAG, "no icon resource found - using application icon");
 			iconId = context.getApplicationInfo().icon;
 		}
 		mBuilder.setSmallIcon(iconId);

 		/*
 		 * Notification Large-Icon
 		 *
 		 * Sets the large-icon of the notification
 		 *
 		 * - checks the gcm data for the `largeIcon` key
 		 * - if none, checks the plugin options for `largeIcon` key
 		 * - if none, we don't set the large icon
 		 *
 		 */
 		int largeIconId = 0;
 		String gcmLargeIcon = extras.getString("largeIcon"); // from gcm
 		if (gcmLargeIcon != null) {
 			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);
 		}


		String message = extras.getString("message");
		if (message != null) {
			mBuilder.setContentText(message);
+24 −1
Changes for src/android/com/adobe/phonegap/push/PushPlugin.java: 24 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package com.adobe.phonegap.push;

import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

@@ -48,11 +49,12 @@ public class PushPlugin extends CordovaPlugin {

		if (INITIALIZE.equals(action)) {
			pushContext = callbackContext;
            JSONObject jo = null;

			Log.v(LOG_TAG, "execute: data=" + data.toString());

			try {
				JSONObject jo = data.getJSONObject(0).getJSONObject("android");
                jo = data.getJSONObject(0).getJSONObject("android");

				gWebView = this.webView;
				Log.v(LOG_TAG, "execute: jo=" + jo.toString());
@@ -69,6 +71,27 @@ public class PushPlugin extends CordovaPlugin {
				callbackContext.error(e.getMessage());
			}

            if (jo != null) {
                SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPref.edit();
                try {
                    editor.putString("icon", jo.getString("icon"));
                } catch (JSONException e) {
                    Log.d(LOG_TAG, "no icon option");
                }
                try {
                    editor.putString("iconColor", jo.getString("iconColor"));
                } 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();
            }

			if ( gCachedExtras != null) {
				Log.v(LOG_TAG, "sending cached extras");
				sendExtras(gCachedExtras);