Commit 8e0983e2 authored by Yusuke Iwaki's avatar Yusuke Iwaki

reactive notification base

parent 21d1e564
...@@ -22,6 +22,8 @@ import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver; ...@@ -22,6 +22,8 @@ import chat.rocket.android.service.observer.GetUsersOfRoomsProcedureObserver;
import chat.rocket.android.service.observer.LoadMessageProcedureObserver; import chat.rocket.android.service.observer.LoadMessageProcedureObserver;
import chat.rocket.android.service.observer.MethodCallObserver; import chat.rocket.android.service.observer.MethodCallObserver;
import chat.rocket.android.service.observer.NewMessageObserver; import chat.rocket.android.service.observer.NewMessageObserver;
import chat.rocket.android.service.observer.ReactiveNotificationCancelManager;
import chat.rocket.android.service.observer.ReactiveNotificationManager;
import chat.rocket.android.service.observer.SessionObserver; import chat.rocket.android.service.observer.SessionObserver;
import chat.rocket.android.service.observer.TokenLoginObserver; import chat.rocket.android.service.observer.TokenLoginObserver;
import chat.rocket.android_ddp.DDPClientCallback; import chat.rocket.android_ddp.DDPClientCallback;
...@@ -45,7 +47,9 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -45,7 +47,9 @@ public class RocketChatWebSocketThread extends HandlerThread {
LoadMessageProcedureObserver.class, LoadMessageProcedureObserver.class,
GetUsersOfRoomsProcedureObserver.class, GetUsersOfRoomsProcedureObserver.class,
NewMessageObserver.class, NewMessageObserver.class,
CurrentUserObserver.class CurrentUserObserver.class,
ReactiveNotificationManager.class,
ReactiveNotificationCancelManager.class
}; };
private final Context appContext; private final Context appContext;
private final String serverConfigId; private final String serverConfigId;
......
package chat.rocket.android.service.observer;
import android.content.Context;
import android.support.v4.app.NotificationManagerCompat;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.realm_helper.RealmHelper;
import hugo.weaving.DebugLog;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
/**
* observing room subscriptions with unread>0.
*/
public class ReactiveNotificationCancelManager extends AbstractModelObserver<RoomSubscription> {
public ReactiveNotificationCancelManager(Context context, String hostname,
RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, hostname, realmHelper, ddpClient);
}
@Override public RealmResults<RoomSubscription> queryItems(Realm realm) {
return realm.where(RoomSubscription.class)
.equalTo("open", true)
.equalTo("unread", 0)
.findAll();
}
@DebugLog
@Override public void onUpdateResults(List<RoomSubscription> roomSubscriptions) {
// TODO implement!
for (RoomSubscription roomSubscription : roomSubscriptions) {
final String roomId = roomSubscription.getRid();
NotificationManagerCompat.from(context).cancel(roomId.hashCode());
}
}
}
package chat.rocket.android.service.observer;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.content.ContextCompat;
import chat.rocket.android.R;
import chat.rocket.android.activity.MainActivity;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
import hugo.weaving.DebugLog;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
/**
* observing room subscriptions with unread>0.
*/
public class ReactiveNotificationManager extends AbstractModelObserver<RoomSubscription> {
public ReactiveNotificationManager(Context context, String hostname,
RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, hostname, realmHelper, ddpClient);
}
@Override public RealmResults<RoomSubscription> queryItems(Realm realm) {
return realm.where(RoomSubscription.class)
.equalTo("open", true)
.greaterThan("unread", 0)
.findAll();
}
@DebugLog
@Override public void onUpdateResults(List<RoomSubscription> roomSubscriptions) {
// TODO implement!
for (RoomSubscription roomSubscription : roomSubscriptions) {
final String roomId = roomSubscription.getRid();
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ServerConfig config = RealmStore.getDefault().executeTransactionForRead(realm ->
realm.where(ServerConfig.class).equalTo("hostname", hostname).findFirst());
if (config != null) {
intent.putExtra("serverConfigId", config.getServerConfigId());
intent.putExtra("roomId", roomId);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(),
(int) (System.currentTimeMillis() % Integer.MAX_VALUE),
intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(roomSubscription.getName())
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.rocket_chat_notification_24dp)
.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManagerCompat.from(context).notify(roomId.hashCode(), notification);
}
}
}
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