Commit 0c274573 authored by Yusuke Iwaki's avatar Yusuke Iwaki

update lastSeenAt on Notification dismissal.

parent 500b0e78
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
android:windowSoftInputMode="adjustResize"/> android:windowSoftInputMode="adjustResize"/>
<service android:name=".service.RocketChatService"/> <service android:name=".service.RocketChatService"/>
<service android:name=".service.notification.NotificationDismissalCallbackService"/>
</application> </application>
</manifest> </manifest>
package chat.rocket.android.service.notification;
import android.app.IntentService;
import android.content.Intent;
import chat.rocket.android.model.internal.NotificationItem;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore;
/**
* triggered when notification is dismissed.
*/
public class NotificationDismissalCallbackService extends IntentService {
public NotificationDismissalCallbackService() {
super(NotificationDismissalCallbackService.class.getSimpleName());
}
@Override protected void onHandleIntent(Intent intent) {
if (!intent.hasExtra("serverConfigId") || !intent.hasExtra("roomId")) {
return;
}
String serverConfigId = intent.getStringExtra("serverConfigId");
String roomId = intent.getStringExtra("roomId");
RealmHelper realmHelper = RealmStore.get(serverConfigId);
if (realmHelper == null) {
return;
}
realmHelper.executeTransaction(realm -> {
NotificationItem item =
realm.where(NotificationItem.class).equalTo("roomId", roomId).findFirst();
if (item != null) {
long currentTime = System.currentTimeMillis();
if (item.getLastSeenAt() <= currentTime) {
item.setLastSeenAt(currentTime);
}
}
return null;
});
}
}
...@@ -20,6 +20,7 @@ import chat.rocket.android.model.ServerConfig; ...@@ -20,6 +20,7 @@ import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.internal.NotificationItem; import chat.rocket.android.model.internal.NotificationItem;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.notification.NotificationDismissalCallbackService;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
import java.util.List; import java.util.List;
...@@ -83,8 +84,7 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification ...@@ -83,8 +84,7 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification
}); });
} }
private Notification generateNotification(String roomId, String title, private PendingIntent getContentIntent(String roomId) {
@NonNull String description, int unreadCount, @Nullable Bitmap icon) {
Intent intent = new Intent(context, MainActivity.class); Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
ServerConfig config = RealmStore.getDefault().executeTransactionForRead(realm -> ServerConfig config = RealmStore.getDefault().executeTransactionForRead(realm ->
...@@ -94,9 +94,27 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification ...@@ -94,9 +94,27 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification
intent.putExtra("roomId", roomId); intent.putExtra("roomId", roomId);
} }
PendingIntent pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), return PendingIntent.getActivity(context.getApplicationContext(),
(int) (System.currentTimeMillis() % Integer.MAX_VALUE), (int) (System.currentTimeMillis() % Integer.MAX_VALUE),
intent, PendingIntent.FLAG_ONE_SHOT); intent, PendingIntent.FLAG_ONE_SHOT);
}
private PendingIntent getDeleteIntent(String roomId) {
Intent intent = new Intent(context, NotificationDismissalCallbackService.class);
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);
}
return PendingIntent.getService(context.getApplicationContext(),
(int) (System.currentTimeMillis() % Integer.MAX_VALUE),
intent, PendingIntent.FLAG_ONE_SHOT);
}
private Notification generateNotification(String roomId, String title,
@NonNull String description, int unreadCount, @Nullable Bitmap icon) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context) NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(title) .setContentTitle(title)
...@@ -104,7 +122,8 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification ...@@ -104,7 +122,8 @@ public class NotificationItemObserver extends AbstractModelObserver<Notification
.setNumber(unreadCount) .setNumber(unreadCount)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary)) .setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setSmallIcon(R.drawable.rocket_chat_notification_24dp) .setSmallIcon(R.drawable.rocket_chat_notification_24dp)
.setContentIntent(pendingIntent); .setContentIntent(getContentIntent(roomId))
.setDeleteIntent(getDeleteIntent(roomId));
if (icon != null) { if (icon != null) {
builder.setLargeIcon(icon); builder.setLargeIcon(icon);
......
...@@ -76,8 +76,20 @@ public class RealmHelper { ...@@ -76,8 +76,20 @@ public class RealmHelper {
} }
} }
private boolean shouldUseSync() {
// ref: realm-java:realm/realm-library/src/main/java/io/realm/AndroidNotifier.java
// #isAutoRefreshAvailable()
if (Looper.myLooper() == null) {
return true;
}
String threadName = Thread.currentThread().getName();
return threadName != null && threadName.startsWith("IntentService[");
}
public Task<Void> executeTransaction(final RealmHelper.Transaction transaction) { public Task<Void> executeTransaction(final RealmHelper.Transaction transaction) {
return Looper.myLooper() == null ? executeTransactionSync(transaction) return shouldUseSync() ? executeTransactionSync(transaction)
: executeTransactionAsync(transaction); : executeTransactionAsync(transaction);
} }
......
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