From 4ddaa4f08f97bea9d901ef8e86f55d940c222e81 Mon Sep 17 00:00:00 2001 From: Paulo Albuquerque Date: Thu, 6 Aug 2015 10:20:30 -0300 Subject: [PATCH] Insert custom large image --- .../com/plugin/gcm/GCMIntentService.java | 253 ++++++++++-------- 1 file changed, 137 insertions(+), 116 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..d1542536 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -1,5 +1,11 @@ package com.plugin.gcm; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + import org.json.JSONException; import org.json.JSONObject; @@ -9,6 +15,8 @@ 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; @@ -18,58 +26,58 @@ @SuppressLint("NewApi") public class GCMIntentService extends GCMBaseIntentService { - private static final String TAG = "GCMIntentService"; - - public GCMIntentService() { - super("GCMIntentService"); - } - - @Override - public void onRegistered(Context context, String regId) { - - Log.v(TAG, "onRegistered: "+ regId); + private static final String TAG = "GCMIntentService"; - JSONObject json; + public GCMIntentService() { + super("GCMIntentService"); + } - try - { - json = new JSONObject().put("event", "registered"); - json.put("regid", regId); + @Override + public void onRegistered(Context context, String regId) { - Log.v(TAG, "onRegistered: " + json.toString()); + Log.v(TAG, "onRegistered: "+ regId); - // 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 ); + JSONObject json; - } - catch( JSONException e) - { - // No message to the user is sent, JSON failed - Log.e(TAG, "onRegistered: JSON exception"); - } - } + try + { + json = new JSONObject().put("event", "registered"); + json.put("regid", regId); - @Override - public void onUnregistered(Context context, String regId) { - Log.d(TAG, "onUnregistered - regId: " + regId); - } + Log.v(TAG, "onRegistered: " + json.toString()); - @Override - protected void onMessage(Context context, Intent intent) { - Log.d(TAG, "onMessage - context: " + context); + // 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 ); - // Extract the payload from the message - Bundle extras = intent.getExtras(); - if (extras != null) - { - // if we are in the foreground, just surface the payload, else post it to the statusbar + } + 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); + + // Extract the payload from the message + Bundle extras = intent.getExtras(); + if (extras != null) + { + // if we are in the foreground, just surface the payload, else post it to the statusbar if (PushPlugin.isInForeground()) { - extras.putBoolean("foreground", true); + extras.putBoolean("foreground", true); PushPlugin.sendExtras(extras); - } - else { - extras.putBoolean("foreground", false); + } + else { + extras.putBoolean("foreground", false); // Send a notification if there is a message if (extras.getString("message") != null && extras.getString("message").length() != 0) { @@ -77,77 +85,90 @@ protected void onMessage(Context context, Intent intent) { } } } - } - - 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); - - int defaults = Notification.DEFAULT_ALL; - - if (extras.getString("defaults") != null) { - try { - defaults = Integer.parseInt(extras.getString("defaults")); - } catch (NumberFormatException e) {} - } - - 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); - - String message = extras.getString("message"); - if (message != null) { - mBuilder.setContentText(message); - } else { - mBuilder.setContentText(""); - } - - String msgcnt = extras.getString("msgcnt"); - if (msgcnt != null) { - mBuilder.setNumber(Integer.parseInt(msgcnt)); - } - - int notId = 0; - - try { - notId = Integer.parseInt(extras.getString("notId")); - } - catch(NumberFormatException e) { - Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage()); - } - catch(Exception e) { - Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); - } - - mNotificationManager.notify((String) appName, notId, mBuilder.build()); - } - - private static String getAppName(Context context) - { - CharSequence appName = - context - .getPackageManager() - .getApplicationLabel(context.getApplicationInfo()); - - return (String)appName; - } - - @Override - public void onError(Context context, String errorId) { - Log.e(TAG, "onError - errorId: " + errorId); - } - -} + } + + 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); + + + Bitmap myBitmap = null; + String customIconUrl= null; + customIconUrl=extras.getString("icon"); + + try { + URL url = new URL(customIconUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.connect(); + InputStream input = connection.getInputStream(); + myBitmap = BitmapFactory.decodeStream(input); + + } catch (IOException e) { + e.printStackTrace(); + + } + + + NotificationCompat.Builder mBuilder = + new NotificationCompat.Builder(context) + .setDefaults(Notification.DEFAULT_ALL) + .setSmallIcon(context.getApplicationInfo().icon) + .setLargeIcon(myBitmap) + .setWhen(System.currentTimeMillis()) + .setContentTitle(extras.getString("title")) + .setTicker(extras.getString("title")) + .setContentIntent(contentIntent) + .setAutoCancel(true); + + String message = extras.getString("message"); + if (message != null) { + mBuilder.setContentText(message); + } else { + mBuilder.setContentText(""); + } + + String msgcnt = extras.getString("msgcnt"); + if (msgcnt != null) { + mBuilder.setNumber(Integer.parseInt(msgcnt)); + } + + int notId = 0; + + try { + notId = Integer.parseInt(extras.getString("notId")); + } + catch(NumberFormatException e) { + Log.e(TAG, "Number format exception - Error parsing Notification ID: " + e.getMessage()); + } + catch(Exception e) { + Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); + } + + mNotificationManager.notify((String) appName, notId, mBuilder.build()); + + } + + private static String getAppName(Context context) + { + CharSequence appName = + context + .getPackageManager() + .getApplicationLabel(context.getApplicationInfo()); + + return (String)appName; + } + + @Override + public void onError(Context context, String errorId) { + Log.e(TAG, "onError - errorId: " + errorId); + } + +} \ No newline at end of file