Commit 5f535d4b authored by Varun Patil's avatar Varun Patil

Add launcher badges for number of notifications

parent 21710850
......@@ -35,6 +35,7 @@ ext {
flexboxVersion = '1.0.0'
sectionedRecyclerViewVersion = '1.2.0'
lottieVersion = '2.7.0'
shortcutBadgerVersion = '1.1.22@aar'
}
dependencies {
......@@ -59,5 +60,6 @@ dependencies {
implementation "com.google.android:flexbox:${flexboxVersion}"
implementation "io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:${sectionedRecyclerViewVersion}"
implementation "com.airbnb.android:lottie:$lottieVersion"
implementation "me.leolin:ShortcutBadger:$shortcutBadgerVersion"
}
apply plugin: 'com.google.gms.google-services'
......@@ -45,6 +45,7 @@ public class Constants {
public static final String FCM_BUNDLE_IMAGE = "image_url";
public static final String FCM_BUNDLE_LARGE_ICON = "large_icon";
public static final String FCM_BUNDLE_LARGE_CONTENT = "large_content";
public static final String FCM_BUNDLE_TOTAL_COUNT = "total_count";
public static final String NOTIF_CANCELLED = "notif_cancelled";
public static final String FCM_BUNDLE_ACTION_STARTING = "starting";
......
......@@ -26,6 +26,7 @@ import java.util.Map;
import app.insti.activity.MainActivity;
import app.insti.notifications.NotificationId;
import me.leolin.shortcutbadger.ShortcutBadger;
public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
String channel;
......@@ -95,6 +96,17 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
.setContentIntent(getNotificationIntent(remoteMessage, notification_id))
.setDeleteIntent(getDeleteIntent(remoteMessage, notification_id));
/* Update the badge */
final String count = remoteMessage.getData().get(Constants.FCM_BUNDLE_TOTAL_COUNT);
if (count != null) {
try {
int total_count = Integer.parseInt(count);
NotificationId.setCurrentCount(total_count);
ShortcutBadger.applyCount(getApplicationContext(), total_count);
}
catch (NumberFormatException ignored) {}
}
/* Check for article */
String largeContent = remoteMessage.getData().get(Constants.FCM_BUNDLE_LARGE_CONTENT);
if (largeContent != null) {
......
......@@ -7,6 +7,8 @@ import android.content.Intent;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.ServiceGenerator;
import app.insti.notifications.NotificationId;
import me.leolin.shortcutbadger.ShortcutBadger;
public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
......@@ -27,6 +29,9 @@ public class NotificationBroadcastReceiver extends BroadcastReceiver {
// Mark as read
retrofitInterface.markNotificationDeleted(Utils.getSessionIDHeader(), id).enqueue(new EmptyCallback<Void>());
// Reduce current count
ShortcutBadger.applyCount(context.getApplicationContext(), NotificationId.decrementAndGetCurrentCount());
}
}
}
......@@ -75,6 +75,8 @@ import app.insti.fragment.QuickLinksFragment;
import app.insti.fragment.SettingsFragment;
import app.insti.fragment.TrainingBlogFragment;
import app.insti.fragment.UserFragment;
import app.insti.notifications.NotificationId;
import me.leolin.shortcutbadger.ShortcutBadger;
import retrofit2.Call;
import retrofit2.Response;
......@@ -195,6 +197,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
Utils.notificationCache = new UpdatableList<>();
Utils.notificationCache.setList(response.body());
showNotifications();
NotificationId.setCurrentCount(Utils.notificationCache.size());
ShortcutBadger.applyCount(getApplicationContext(), NotificationId.getCurrentCount());
}
}
});
......
......@@ -19,6 +19,8 @@ import app.insti.fragment.NewsFragment;
import app.insti.fragment.NotificationsFragment;
import app.insti.fragment.PlacementBlogFragment;
import app.insti.fragment.TrainingBlogFragment;
import app.insti.notifications.NotificationId;
import me.leolin.shortcutbadger.ShortcutBadger;
public class NotificationsAdapter extends CardAdapter<Notification> {
private NotificationsFragment notificationsFragment;
......@@ -34,6 +36,7 @@ public class NotificationsAdapter extends CardAdapter<Notification> {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
String sessId = Utils.getSessionIDHeader();
retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>());
ShortcutBadger.applyCount(fragmentActivity.getApplicationContext(), NotificationId.decrementAndGetCurrentCount());
/* Close the bottom sheet */
notificationsFragment.dismiss();
......
......@@ -19,6 +19,8 @@ import app.insti.adapter.NotificationsAdapter;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Notification;
import app.insti.notifications.NotificationId;
import me.leolin.shortcutbadger.ShortcutBadger;
import retrofit2.Call;
import retrofit2.Response;
......@@ -61,6 +63,9 @@ public class NotificationsFragment extends BottomSheetDialogFragment {
if (response.isSuccessful()) {
Utils.notificationCache.setList(response.body());
showNotifications(Utils.notificationCache);
NotificationId.setCurrentCount(Utils.notificationCache.size());
ShortcutBadger.applyCount(getContext().getApplicationContext(), NotificationId.getCurrentCount());
}
}
});
......
......@@ -7,4 +7,20 @@ public class NotificationId {
public static int getID() {
return c.incrementAndGet();
}
private final static AtomicInteger current_count = new AtomicInteger(0);
public static int getCurrentCount() {
return c.get();
}
public static int decrementAndGetCurrentCount() {
if (c.get() > 0)
return c.decrementAndGet();
else
return 0;
}
public static void setCurrentCount(int count) {
c.set(count);
}
}
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