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

Add launcher badges for number of notifications

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