From 653909784b84df0e9414735cd717869c987327e8 Mon Sep 17 00:00:00 2001 From: eros2187 Date: Tue, 11 Aug 2015 17:11:57 +0200 Subject: [PATCH] big_photo --- .../com/plugin/gcm/GCMIntentService.java | 113 ++++++++++++++++-- 1 file changed, 101 insertions(+), 12 deletions(-) diff --git a/src/android/com/plugin/gcm/GCMIntentService.java b/src/android/com/plugin/gcm/GCMIntentService.java index caee145e..b0b4b876 100644 --- a/src/android/com/plugin/gcm/GCMIntentService.java +++ b/src/android/com/plugin/gcm/GCMIntentService.java @@ -1,14 +1,20 @@ 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; - import android.annotation.SuppressLint; import android.app.Notification; 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; @@ -19,7 +25,7 @@ public class GCMIntentService extends GCMBaseIntentService { private static final String TAG = "GCMIntentService"; - + public GCMIntentService() { super("GCMIntentService"); } @@ -73,7 +79,12 @@ protected void onMessage(Context context, Intent intent) { // Send a notification if there is a message if (extras.getString("message") != null && extras.getString("message").length() != 0) { - createNotification(context, extras); + if (extras.getString("bigPicture") != null) { + createBigPicNotification(context, extras); + } + else { + createNotification(context, extras); + } } } } @@ -89,7 +100,7 @@ public void createNotification(Context context, Bundle extras) 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) { @@ -97,7 +108,7 @@ public void createNotification(Context context, Bundle extras) defaults = Integer.parseInt(extras.getString("defaults")); } catch (NumberFormatException e) {} } - + NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setDefaults(defaults) @@ -119,9 +130,9 @@ public void createNotification(Context context, Bundle extras) if (msgcnt != null) { mBuilder.setNumber(Integer.parseInt(msgcnt)); } - + int notId = 0; - + try { notId = Integer.parseInt(extras.getString("notId")); } @@ -131,20 +142,98 @@ public void createNotification(Context context, Bundle extras) catch(Exception e) { Log.e(TAG, "Number format exception - Error parsing Notification ID" + e.getMessage()); } - + mNotificationManager.notify((String) appName, notId, mBuilder.build()); } - + + public void createBigPicNotification(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) {} + } + + String bigPictureUrl= null; + bigPictureUrl=extras.getString("bigPicture"); + Bitmap bigPictureBMP = null; + if (bigPictureUrl != null) { + try { + URL url = new URL(bigPictureUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.connect(); + InputStream input = connection.getInputStream(); + bigPictureBMP = BitmapFactory.decodeStream(input); + } catch (IOException e) { + e.printStackTrace(); + } + } + + NotificationCompat.BigPictureStyle bigPicStyle = new + NotificationCompat.BigPictureStyle(); + bigPicStyle.setBigContentTitle(extras.getString("title")); + bigPicStyle.setSummaryText(extras.getString("message")); + bigPicStyle.bigPicture(bigPictureBMP); + + NotificationCompat.Builder mBuilder = + new NotificationCompat.Builder(context) + .setDefaults(defaults) + .setSmallIcon(context.getApplicationInfo().icon) + .setWhen(System.currentTimeMillis()) + .setTicker(extras.getString("title")) + .setContentIntent(contentIntent) + .setAutoCancel(true) + .setStyle(bigPicStyle); + + 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 = + CharSequence appName = context .getPackageManager() .getApplicationLabel(context.getApplicationInfo()); - + return (String)appName; } - + @Override public void onError(Context context, String errorId) { Log.e(TAG, "onError - errorId: " + errorId);