Bladeren bron

Implemented attachments and big picture style for Android

Sebastián Katzer 8 jaren geleden
bovenliggende
commit
d9bb523c6d
2 gewijzigde bestanden met toevoegingen van 55 en 17 verwijderingen
  1. 17 15
      src/android/notification/Builder.java
  2. 38 2
      src/android/notification/Options.java

+ 17 - 15
src/android/notification/Builder.java

@@ -24,8 +24,10 @@ package de.appplant.cordova.plugin.notification;
 import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
+import android.graphics.Bitmap;
 import android.support.v4.app.NotificationCompat;
 
+import java.util.List;
 import java.util.Random;
 
 import de.appplant.cordova.plugin.notification.activity.ClickActivity;
@@ -130,20 +132,24 @@ public class Builder {
      * @param builder Local notification builder instance
      */
     private void applyStyle(NotificationCompat.Builder builder) {
-        String summary = options.getSummary();
-        String text    = options.getText();
+        List<Bitmap> pics = options.getAttachments();
+        String summary    = options.getSummary();
+        String text       = options.getText();
 
-        if (summary == null && text == null)
+        if (pics.size() > 0) {
+            NotificationCompat.BigPictureStyle style =
+                    new NotificationCompat.BigPictureStyle(builder)
+                            .setSummaryText(summary == null ? text : summary)
+                            .bigPicture(pics.get(0));
+
+            builder.setStyle(style);
             return;
+        }
 
-        if (text.contains("\n")) {
+        if (text != null && text.contains("\n")) {
             NotificationCompat.InboxStyle style =
                     new NotificationCompat.InboxStyle(builder)
-                            .setBigContentTitle(options.getTitle());
-
-            if (summary != null) {
-                style.setSummaryText(summary);
-            }
+                            .setSummaryText(summary);
 
             for (String line : text.split("\n")) {
                 style.addLine(line);
@@ -153,18 +159,14 @@ public class Builder {
             return;
         }
 
-        if (summary == null && text.length() < 45)
+        if (text == null || summary == null && text.length() < 45)
             return;
 
         NotificationCompat.BigTextStyle style =
                 new NotificationCompat.BigTextStyle(builder)
-                        .setBigContentTitle(options.getTitle())
+                        .setSummaryText(summary)
                         .bigText(text);
 
-        if (summary != null) {
-            style.setSummaryText(summary);
-        }
-
         builder.setStyle(style);
     }
 

+ 38 - 2
src/android/notification/Options.java

@@ -29,6 +29,11 @@ import android.support.v4.app.NotificationCompat;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.List;
+
 import de.appplant.cordova.plugin.notification.util.AssetUtil;
 
 import static android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS;
@@ -249,7 +254,7 @@ public class Options {
      * Sound file path for the local notification.
      */
     public Uri getSound() {
-        return assets.parse(options.optString("sound"));
+        return assets.parse(options.optString("sound", null));
     }
 
     /**
@@ -448,7 +453,38 @@ public class Options {
      * The summary for inbox style notifications.
      */
     String getSummary() {
-        return options.optString("summary");
+        return options.optString("summary", null);
+    }
+
+    /**
+     * Image attachments for image style notifications.
+     *
+     * @return For now it only returns the first item as Android does not
+     *         support multiple attachments like iOS.
+     */
+    List<Bitmap> getAttachments() {
+        JSONArray paths   = options.optJSONArray("attachments");
+        List<Bitmap> pics = new ArrayList<Bitmap>();
+
+        if (paths == null)
+            return pics;
+
+        for (int i = 0; i < paths.length(); i++) {
+            Uri uri = assets.parse(paths.optString(i));
+
+            if (uri == Uri.EMPTY)
+                continue;
+
+            try {
+                Bitmap pic = assets.getIconFromUri(uri);
+                pics.add(pic);
+                break;
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
+        return pics;
     }
 
     /**