Commit 54da7fb2 authored by Yusuke Iwaki's avatar Yusuke Iwaki

notification base implementation.

parent 7c464ecc
package chat.rocket.android.notification;
/**
* notifier.
*/
public interface Notifier {
void publishNotificationIfNeeded();
}
package chat.rocket.android.notification;
import android.app.Notification;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import chat.rocket.android.R;
import org.json.JSONObject;
/**
* utility class for notification.
*/
public class StreamNotifyUserNotifier implements Notifier {
private final Context context;
private final String title;
private final String text;
private final JSONObject payload;
public StreamNotifyUserNotifier(Context context, String title, String text, JSONObject payload) {
this.context = context;
this.title = title;
this.text = text;
this.payload = payload;
}
@Override public void publishNotificationIfNeeded() {
if (!shouldNotify()) {
return;
}
NotificationManagerCompat.from(context)
.notify(generateNotificationId(), generateNotification());
}
private boolean shouldNotify() {
// TODO: should check if target message is already read or not.
return true;
}
private int generateNotificationId() {
// TODO: should summary notification by user or room.
return (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
}
private Notification generateNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(true)
.setSmallIcon(R.drawable.rocket_chat_notification_24dp);
if (text.length() > 20) {
return new NotificationCompat.BigTextStyle(builder)
.bigText(text)
.build();
} else {
return builder.build();
}
}
}
......@@ -7,6 +7,7 @@ import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.service.ddp.AbstractDDPDocEventSubscriber;
import chat.rocket.android_ddp.DDPSubscription;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import timber.log.Timber;
......@@ -24,11 +25,35 @@ abstract class AbstractStreamNotifyEventSubscriber extends AbstractDDPDocEventSu
return getSubscriptionName().equals(callbackName);
}
protected abstract String getSubscriptionParam();
@Override protected final JSONArray getSubscriptionParams() throws JSONException {
return new JSONArray().put(getSubscriptionParam()).put(false);
}
protected abstract String getPrimaryKeyForModel();
@Override protected void onDocumentChanged(DDPSubscription.Changed docEvent) {
@Override protected final void onDocumentAdded(DDPSubscription.Added docEvent) {
// do nothing.
}
@Override protected final void onDocumentRemoved(DDPSubscription.Removed docEvent) {
// do nothing.
}
@Override protected final void onDocumentChanged(DDPSubscription.Changed docEvent) {
try {
JSONArray args = docEvent.fields.getJSONArray("args");
if (!docEvent.fields.getString("eventName").equals(getSubscriptionParam())) {
return;
}
handleArgs(docEvent.fields.getJSONArray("args"));
} catch (Exception exception) {
Timber.w(exception, "failed to save stream-notify event.");
}
}
protected void handleArgs(JSONArray args) throws JSONException {
String msg = args.length() > 0 ? args.getString(0) : null;
JSONObject target = args.getJSONObject(args.length() - 1);
if ("removed".equals(msg)) {
......@@ -42,8 +67,5 @@ abstract class AbstractStreamNotifyEventSubscriber extends AbstractDDPDocEventSu
realm.createOrUpdateObjectFromJson(getModelClass(), customizeFieldJson(target))
).continueWith(new LogcatIfError());
}
} catch (Exception exception) {
Timber.w(exception, "failed to save stream-notify event.");
}
}
}
package chat.rocket.android.service.ddp.stream;
import android.content.Context;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.realm_helper.RealmHelper;
abstract class AbstractStreamNotifyUserEventSubscriber extends AbstractStreamNotifyEventSubscriber {
protected final String userId;
protected AbstractStreamNotifyUserEventSubscriber(Context context, RealmHelper realmHelper,
DDPClientWraper ddpClient, String userId) {
super(context, realmHelper, ddpClient);
this.userId = userId;
}
@Override protected final String getSubscriptionName() {
return "stream-notify-user";
}
@Override protected final String getSubscriptionParam() {
return userId + "/" + getSubscriptionSubParam();
}
protected abstract String getSubscriptionSubParam();
}
package chat.rocket.android.service.ddp.stream;
import android.content.Context;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.notification.Notifier;
import chat.rocket.android.notification.StreamNotifyUserNotifier;
import chat.rocket.android.realm_helper.RealmHelper;
import io.realm.RealmObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class StreamNotifyUserNotification extends AbstractStreamNotifyUserEventSubscriber {
public StreamNotifyUserNotification(Context context, RealmHelper realmHelper,
DDPClientWraper ddpClient, String userId) {
super(context, realmHelper, ddpClient, userId);
}
@Override protected String getSubscriptionSubParam() {
return "notification";
}
@Override protected void handleArgs(JSONArray args) throws JSONException {
JSONObject target = args.getJSONObject(args.length() - 1);
Notifier notifier = new StreamNotifyUserNotifier(context,
target.getString("title"),
target.getString("text"),
target.getJSONObject("payload"));
notifier.publishNotificationIfNeeded();
}
@Override protected Class<? extends RealmObject> getModelClass() {
// not used because handleArgs is override.
return null;
}
@Override protected String getPrimaryKeyForModel() {
// not used because handleArgs is override.
return null;
}
}
......@@ -5,26 +5,15 @@ import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.realm_helper.RealmHelper;
import io.realm.RealmObject;
import org.json.JSONArray;
import org.json.JSONException;
public class StreamNotifyUserSubscriptionsChanged extends AbstractStreamNotifyEventSubscriber {
private final String userId;
public class StreamNotifyUserSubscriptionsChanged extends AbstractStreamNotifyUserEventSubscriber {
public StreamNotifyUserSubscriptionsChanged(Context context, RealmHelper realmHelper,
DDPClientWraper ddpClient, String userId) {
super(context, realmHelper, ddpClient);
this.userId = userId;
}
@Override protected String getSubscriptionName() {
return "stream-notify-user";
super(context, realmHelper, ddpClient, userId);
}
@Override protected JSONArray getSubscriptionParams() throws JSONException {
return new JSONArray()
.put(userId + "/subscriptions-changed")
.put(false);
@Override protected String getSubscriptionSubParam() {
return "subscriptions-changed";
}
@Override protected Class<? extends RealmObject> getModelClass() {
......
......@@ -5,7 +5,6 @@ import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.realm_helper.RealmHelper;
import io.realm.RealmObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -25,10 +24,8 @@ public class StreamRoomMessage extends AbstractStreamNotifyEventSubscriber {
return "stream-room-messages";
}
@Override protected JSONArray getSubscriptionParams() throws JSONException {
return new JSONArray()
.put(roomId)
.put(false);
@Override protected String getSubscriptionParam() {
return roomId;
}
@Override protected Class<? extends RealmObject> getModelClass() {
......
......@@ -6,6 +6,7 @@ import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.service.Registerable;
import chat.rocket.android.service.ddp.stream.StreamNotifyUserNotification;
import chat.rocket.android.service.ddp.stream.StreamNotifyUserSubscriptionsChanged;
import hugo.weaving.DebugLog;
import io.realm.Realm;
......@@ -64,6 +65,11 @@ public class CurrentUserObserver extends AbstractModelObserver<User> {
listeners.add(listener);
return null;
});
Registerable listener = new StreamNotifyUserNotification(
context, realmHelper, ddpClient, userId);
listener.register();
listeners.add(listener);
}
@DebugLog
......
<vector android:height="24dp" android:viewportHeight="48.0"
android:viewportWidth="48.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#A3000000"
android:pathData="M44.99,23.47C44.99,21.42 44.38,19.45 43.16,17.62C42.07,15.97 40.54,14.52 38.62,13.29C34.91,10.92 30.03,9.62 24.88,9.62C23.16,9.62 21.47,9.77 19.82,10.05C18.8,9.1 17.61,8.24 16.35,7.57C9.6,4.3 4,7.49 4,7.49C4,7.49 9.21,11.76 8.36,15.49C6.03,17.8 4.77,20.58 4.77,23.47C4.77,23.48 4.77,23.49 4.77,23.5C4.77,23.51 4.77,23.51 4.77,23.52C4.77,26.42 6.03,29.2 8.36,31.5C9.21,35.24 4,39.5 4,39.5C4,39.5 9.6,42.69 16.35,39.43C17.61,38.75 18.8,37.89 19.82,36.94C21.47,37.23 23.16,37.37 24.88,37.37C30.03,37.37 34.91,36.07 38.62,33.7C40.54,32.48 42.07,31.02 43.16,29.38C44.38,27.55 44.99,25.58 44.99,23.53C44.99,23.52 44.99,23.51 44.99,23.5L44.99,23.47ZM24.88,12.53C34.41,12.53 42.14,17.45 42.14,23.52C42.14,29.6 34.41,34.52 24.88,34.52C22.76,34.52 20.73,34.28 18.85,33.83C16.94,36.12 12.74,39.31 8.67,38.28C9.99,36.86 11.96,34.45 11.54,30.5C9.09,28.6 7.63,26.17 7.63,23.52C7.63,17.45 15.35,12.53 24.88,12.53Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
<path android:fillColor="#A3000000"
android:pathData="M24.88,26.17C26.15,26.17 27.17,25.14 27.17,23.88C27.17,22.61 26.15,21.59 24.88,21.59C23.62,21.59 22.59,22.61 22.59,23.88C22.59,25.14 23.62,26.17 24.88,26.17ZM32.85,26.17C34.12,26.17 35.14,25.14 35.14,23.88C35.14,22.61 34.12,21.59 32.85,21.59C31.59,21.59 30.56,22.61 30.56,23.88C30.56,25.14 31.59,26.17 32.85,26.17ZM16.91,26.17C18.18,26.17 19.2,25.14 19.2,23.88C19.2,22.62 18.18,21.59 16.91,21.59C15.65,21.59 14.62,22.62 14.62,23.88C14.62,25.14 15.65,26.17 16.91,26.17L16.91,26.17Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
<path android:fillColor="#33000000"
android:pathData="M24.88,33.08C22.76,33.08 20.73,32.86 18.85,32.48C17.17,34.23 13.69,36.59 10.1,36.5C9.62,37.22 9.11,37.8 8.67,38.28C12.74,39.31 16.94,36.12 18.85,33.83C20.73,34.28 22.76,34.52 24.88,34.52C34.34,34.52 42.01,29.68 42.13,23.67C42.01,28.88 34.34,33.08 24.88,33.08L24.88,33.08Z"
android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>
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