Commit 402e3a09 by Sebastián Katzer

Support for messaging styled notifications

parent 2ca56e6c
...@@ -41,11 +41,11 @@ public class ClickActivity extends de.appplant.cordova.plugin.notification.activ ...@@ -41,11 +41,11 @@ public class ClickActivity extends de.appplant.cordova.plugin.notification.activ
super.onClick(notification); super.onClick(notification);
// if (notification.getOptions().isOngoing()) if (notification.getOptions().isSticky())
// return; return;
// String event = notification.isRepeating() ? "clear" : "cancel"; String event = notification.isRepeating() ? "clear" : "cancel";
// LocalNotification.fireEvent(event, notification); LocalNotification.fireEvent(event, notification);
} }
} }
...@@ -26,6 +26,7 @@ import android.content.Context; ...@@ -26,6 +26,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -137,43 +138,108 @@ public final class Builder { ...@@ -137,43 +138,108 @@ public final class Builder {
/** /**
* Find out and set the notification style. * Find out and set the notification style.
* *
* @param builder Local notification builder instance * @param builder Local notification builder instance.
*/ */
private void applyStyle(NotificationCompat.Builder builder) { private void applyStyle(NotificationCompat.Builder builder) {
Message[] messages = options.getMessages();
String summary = options.getSummary();
if (messages != null) {
applyMessagingStyle(builder, messages);
return;
}
List<Bitmap> pics = options.getAttachments(); List<Bitmap> pics = options.getAttachments();
if (pics.size() > 0) {
applyBigPictureStyle(builder, pics);
return;
}
String text = options.getText();
if (text != null && text.contains("\n")) {
applyInboxStyle(builder);
return;
}
if (text == null || summary == null && text.length() < 45)
return;
applyBigTextStyle(builder);
}
/**
* Apply inbox style.
*
* @param builder Local notification builder instance.
* @param messages The messages to add to the conversation.
*/
private void applyMessagingStyle(NotificationCompat.Builder builder,
Message[] messages) {
NotificationCompat.MessagingStyle style;
style = new NotificationCompat.MessagingStyle("Me")
.setConversationTitle(options.getTitle());
for (Message msg : messages) {
style.addMessage(msg);
}
builder.setStyle(style);
}
/**
* Apply inbox style.
*
* @param builder Local notification builder instance.
* @param pics The pictures to show.
*/
private void applyBigPictureStyle(NotificationCompat.Builder builder,
List<Bitmap> pics) {
NotificationCompat.BigPictureStyle style;
String summary = options.getSummary(); String summary = options.getSummary();
String text = options.getText(); String text = options.getText();
if (pics.size() > 0) { style = new NotificationCompat.BigPictureStyle(builder)
NotificationCompat.BigPictureStyle style =
new NotificationCompat.BigPictureStyle(builder)
.setSummaryText(summary == null ? text : summary) .setSummaryText(summary == null ? text : summary)
.bigPicture(pics.get(0)); .bigPicture(pics.get(0));
builder.setStyle(style); builder.setStyle(style);
return;
} }
if (text != null && text.contains("\n")) { /**
NotificationCompat.InboxStyle style = * Apply inbox style.
new NotificationCompat.InboxStyle(builder) *
.setSummaryText(summary); * @param builder Local notification builder instance.
*/
private void applyInboxStyle(NotificationCompat.Builder builder) {
NotificationCompat.InboxStyle style;
String text = options.getText();
style = new NotificationCompat.InboxStyle(builder)
.setSummaryText(options.getSummary());
for (String line : text.split("\n")) { for (String line : text.split("\n")) {
style.addLine(line); style.addLine(line);
} }
builder.setStyle(style); builder.setStyle(style);
return;
} }
if (text == null || summary == null && text.length() < 45) /**
return; * Apply big text style.
*
* @param builder Local notification builder instance.
*/
private void applyBigTextStyle(NotificationCompat.Builder builder) {
NotificationCompat.BigTextStyle style;
NotificationCompat.BigTextStyle style = style = new NotificationCompat.BigTextStyle(builder)
new NotificationCompat.BigTextStyle(builder) .setSummaryText(options.getSummary())
.setSummaryText(summary) .bigText(options.getText());
.bigText(text);
builder.setStyle(style); builder.setStyle(style);
} }
...@@ -182,7 +248,7 @@ public final class Builder { ...@@ -182,7 +248,7 @@ public final class Builder {
* Set intent to handle the delete event. Will clean up some persisted * Set intent to handle the delete event. Will clean up some persisted
* preferences. * preferences.
* *
* @param builder Local notification builder instance * @param builder Local notification builder instance.
*/ */
private void applyDeleteReceiver(NotificationCompat.Builder builder) { private void applyDeleteReceiver(NotificationCompat.Builder builder) {
...@@ -203,7 +269,7 @@ public final class Builder { ...@@ -203,7 +269,7 @@ public final class Builder {
* Set intent to handle the click event. Will bring the app to * Set intent to handle the click event. Will bring the app to
* foreground. * foreground.
* *
* @param builder Local notification builder instance * @param builder Local notification builder instance.
*/ */
private void applyContentReceiver(NotificationCompat.Builder builder) { private void applyContentReceiver(NotificationCompat.Builder builder) {
...@@ -225,7 +291,7 @@ public final class Builder { ...@@ -225,7 +291,7 @@ public final class Builder {
/** /**
* Add all actions to the builder if there are any actions. * Add all actions to the builder if there are any actions.
* *
* @param builder Local notification builder instance * @param builder Local notification builder instance.
*/ */
private void applyActions (NotificationCompat.Builder builder) { private void applyActions (NotificationCompat.Builder builder) {
Action[] actions = options.getActions(); Action[] actions = options.getActions();
......
...@@ -108,19 +108,12 @@ public final class Notification { ...@@ -108,19 +108,12 @@ public final class Notification {
return options.getId(); return options.getId();
} }
// /** /**
// * If it's a repeating notification. * If it's a repeating notification.
// */ */
// public boolean isRepeating () { public boolean isRepeating () {
// return getOptions().getRepeatInterval() > 0; return getOptions().getTrigger().has("every");
// } }
// /**
// * If the notification was in the past.
// */
// public boolean wasInThePast () {
// return new Date().after(options.getTriggerDate());
// }
// /** // /**
// * If the notification is scheduled. // * If the notification is scheduled.
...@@ -160,50 +153,26 @@ public final class Notification { ...@@ -160,50 +153,26 @@ public final class Notification {
// } // }
/** /**
* Schedule the local notification. * Clear the local notification without canceling repeating alarms.
*/ */
public void schedule() { public void clear () {
// long triggerTime = options.getTriggerTime();
// persist();
// // Intent gets called when the Notification gets fired
// Intent intent = new Intent(context, receiver)
// .setAction(options.getIdStr())
// .putExtra(Options.EXTRA, options.toString());
// PendingIntent pi = PendingIntent.getBroadcast(
// context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// if (isRepeating()) {
// getAlarmMgr().setRepeating(AlarmManager.RTC_WAKEUP,
// triggerTime, options.getRepeatInterval(), pi);
// } else {
// getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
// }
}
// /**
// * Clear the local notification without canceling repeating alarms.
// */
// public void clear () {
// if (!isRepeating() && wasInThePast()) // if (!isRepeating() && wasInThePast())
// unpersist(); // unpersist();
// if (!isRepeating()) // if (!isRepeating())
// getNotMgr().cancel(getId()); // getNotMgr().cancel(getId());
// } }
// /** /**
// * Cancel the local notification. * Cancel the local notification.
// * *
// * Create an intent that looks similar, to the one that was registered * Create an intent that looks similar, to the one that was registered
// * using schedule. Making sure the notification id in the action is the * using schedule. Making sure the notification id in the action is the
// * same. Now we can search for such an intent using the 'getService' * same. Now we can search for such an intent using the 'getService'
// * method and cancel it. * method and cancel it.
// */ */
// public void cancel() { public void cancel() {
// Intent intent = new Intent(context, receiver) // Intent intent = new Intent(context, receiver)
// .setAction(options.getIdStr()); // .setAction(options.getIdStr());
...@@ -214,7 +183,7 @@ public final class Notification { ...@@ -214,7 +183,7 @@ public final class Notification {
// getNotMgr().cancel(options.getId()); // getNotMgr().cancel(options.getId());
// unpersist(); // unpersist();
// } }
/** /**
* Present the local notification to user. * Present the local notification to user.
...@@ -249,58 +218,43 @@ public final class Notification { ...@@ -249,58 +218,43 @@ public final class Notification {
// return (int) ((now - triggerTime) / options.getRepeatInterval()); // return (int) ((now - triggerTime) / options.getRepeatInterval());
// } // }
// /** /**
// * Encode options to JSON. * Encode options to JSON.
// */ */
// public String toString() { public String toString() {
// JSONObject dict = options.getDict(); JSONObject dict = options.getDict();
// JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
// try { try {
// json = new JSONObject(dict.toString()); json = new JSONObject(dict.toString());
// } catch (JSONException e) { } catch (JSONException e) {
// e.printStackTrace(); e.printStackTrace();
// } }
// json.remove("firstAt");
// json.remove("updated");
// json.remove("soundUri");
// json.remove("iconUri");
// return json.toString();
// }
// /**
// * Persist the information of this notification to the Android Shared
// * Preferences. This will allow the application to restore the notification
// * upon device reboot, app restart, retrieve notifications, aso.
// */
// private void persist () {
// SharedPreferences.Editor editor = getPrefs().edit();
// editor.putString(options.getIdStr(), options.toString()); return json.toString();
}
// if (Build.VERSION.SDK_INT < 9) { /**
// editor.commit(); * Persist the information of this notification to the Android Shared
// } else { * Preferences. This will allow the application to restore the notification
// editor.apply(); * upon device reboot, app restart, retrieve notifications, aso.
// } */
// } private void persist () {
SharedPreferences.Editor editor = getPrefs().edit();
// /** editor.putString(options.getIdentifier(), options.toString());
// * Remove the notification from the Android shared Preferences. editor.apply();
// */ }
// private void unpersist () {
// SharedPreferences.Editor editor = getPrefs().edit();
// editor.remove(options.getIdStr()); /**
* Remove the notification from the Android shared Preferences.
*/
private void unpersist () {
SharedPreferences.Editor editor = getPrefs().edit();
// if (Build.VERSION.SDK_INT < 9) { editor.remove(options.getIdentifier());
// editor.commit(); editor.apply();
// } else { }
// editor.apply();
// }
// }
/** /**
* Shared private preferences for the application. * Shared private preferences for the application.
......
...@@ -26,12 +26,14 @@ import android.graphics.Bitmap; ...@@ -26,12 +26,14 @@ import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.net.Uri; import android.net.Uri;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import de.appplant.cordova.plugin.notification.util.AssetUtil; import de.appplant.cordova.plugin.notification.util.AssetUtil;
...@@ -122,13 +124,6 @@ public final class Options { ...@@ -122,13 +124,6 @@ public final class Options {
} }
/** /**
* Text for the local notification.
*/
public String getText() {
return options.optString("text", "");
}
/**
* Badge number for the local notification. * Badge number for the local notification.
*/ */
int getBadgeNumber() { int getBadgeNumber() {
...@@ -138,7 +133,7 @@ public final class Options { ...@@ -138,7 +133,7 @@ public final class Options {
/** /**
* ongoing flag for local notifications. * ongoing flag for local notifications.
*/ */
Boolean isSticky() { public Boolean isSticky() {
return options.optBoolean("sticky", false); return options.optBoolean("sticky", false);
} }
...@@ -150,6 +145,42 @@ public final class Options { ...@@ -150,6 +145,42 @@ public final class Options {
} }
/** /**
* Gets the raw trigger spec as provided by the user.
*/
public JSONObject getTrigger() {
return options.optJSONObject("trigger");
}
/**
* Gets the value of the silent flag.
*/
boolean isSilent() {
return options.optBoolean("silent", false);
}
/**
* The group for that notification.
*/
String getGroup() {
return options.optString("group", null);
}
/**
* If the group shall show a summary.
*/
boolean getGroupSummary() {
return options.optBoolean("groupSummary", false);
}
/**
* Text for the local notification.
*/
public String getText() {
Object text = options.opt("text");
return text instanceof String ? (String) text : "";
}
/**
* Title for the local notification. * Title for the local notification.
*/ */
public String getTitle() { public String getTitle() {
...@@ -498,27 +529,6 @@ public final class Options { ...@@ -498,27 +529,6 @@ public final class Options {
} }
/** /**
* The group for that notification.
*/
String getGroup() {
return options.optString("group", null);
}
/**
* If the group shall show a summary.
*/
boolean getGroupSummary() {
return options.optBoolean("groupSummary", false);
}
/**
* Gets the value of the silent flag.
*/
boolean isSilent() {
return options.optBoolean("silent", false);
}
/**
* Gets the list of actions to display. * Gets the list of actions to display.
*/ */
Action[] getActions() { Action[] getActions() {
...@@ -543,10 +553,34 @@ public final class Options { ...@@ -543,10 +553,34 @@ public final class Options {
} }
/** /**
* Gets the raw trigger spec as provided by the user. * Gets the list of messages to display.
*
* @return null if there are no messages.
*/ */
public JSONObject getTrigger() { Message[] getMessages() {
return options.optJSONObject("trigger"); Object value = options.opt("text");
if (value == null || value instanceof String)
return null;
JSONArray list = (JSONArray) value;
if (list.length() == 0)
return null;
Message[] messages = new Message[list.length()];
long now = new Date().getTime();
for (int i = 0; i < messages.length; i++) {
JSONObject msg = list.optJSONObject(i);
String text = msg.optString("text");
long timestamp = msg.optLong("date", now);
String person = msg.optString("person", null);
messages[i] = new Message(text, timestamp, person);
}
return messages;
} }
/** /**
......
...@@ -40,11 +40,11 @@ public class ClickActivity extends AbstractClickActivity { ...@@ -40,11 +40,11 @@ public class ClickActivity extends AbstractClickActivity {
public void onClick(Notification notification) { public void onClick(Notification notification) {
launchApp(); launchApp();
// if (notification.isRepeating()) { if (notification.isRepeating()) {
// notification.clear(); notification.clear();
// } else { } else {
// notification.cancel(); notification.cancel();
// } }
} }
} }
...@@ -37,7 +37,7 @@ public class ClearReceiver extends AbstractClearReceiver { ...@@ -37,7 +37,7 @@ public class ClearReceiver extends AbstractClearReceiver {
*/ */
@Override @Override
public void onClear (Notification notification) { public void onClear (Notification notification) {
// notification.clear(); notification.clear();
} }
} }
...@@ -141,10 +141,6 @@ exports.convertProperties = function (options) { ...@@ -141,10 +141,6 @@ exports.convertProperties = function (options) {
options.title = options.title.toString(); options.title = options.title.toString();
} }
if (options.text) {
options.text = options.text.toString();
}
if (options.badge) { if (options.badge) {
options.badge = parseToInt('badge', options); options.badge = parseToInt('badge', options);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment