You need to sign in or sign up before continuing.
Commit 3df7ad16 authored by Varun Patil's avatar Varun Patil

Make delete call to server when notification is swiped away

parent 8e8faaff
...@@ -99,6 +99,12 @@ ...@@ -99,6 +99,12 @@
</intent-filter> </intent-filter>
</service> </service>
<receiver android:name=".NotificationBroadcastReceiver">
<intent-filter>
<action android:name="notification_cancelled"/>
</intent-filter>
</receiver>
<meta-data <meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id" android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" /> android:value="@string/default_notification_channel_id" />
......
...@@ -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 NOTIF_CANCELLED = "notif_cancelled";
public static final String FCM_BUNDLE_ACTION_STARTING = "starting"; public static final String FCM_BUNDLE_ACTION_STARTING = "starting";
......
...@@ -53,6 +53,14 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService { ...@@ -53,6 +53,14 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
return PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntent.getActivity(this, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
/** In case the notification is dismissed */
protected PendingIntent getDeleteIntent(RemoteMessage remoteMessage) {
Intent intent = new Intent(getApplicationContext(), NotificationBroadcastReceiver.class);
intent.setAction(Constants.NOTIF_CANCELLED);
intent.putExtra(Constants.FCM_BUNDLE_NOTIFICATION_ID, remoteMessage.getData().get(Constants.FCM_BUNDLE_NOTIFICATION_ID));
return PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
@Override @Override
public void onMessageReceived(RemoteMessage remoteMessage) { public void onMessageReceived(RemoteMessage remoteMessage) {
channel = getResources().getString(R.string.default_notification_channel_id); channel = getResources().getString(R.string.default_notification_channel_id);
...@@ -84,7 +92,8 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService { ...@@ -84,7 +92,8 @@ public class InstiAppFirebaseMessagingService extends FirebaseMessagingService {
NotificationCompat.Builder builder = standardNotificationBuilder() NotificationCompat.Builder builder = standardNotificationBuilder()
.setContentTitle(remoteMessage.getData().get(Constants.FCM_BUNDLE_TITLE)) .setContentTitle(remoteMessage.getData().get(Constants.FCM_BUNDLE_TITLE))
.setContentText(message) .setContentText(message)
.setContentIntent(getNotificationIntent(remoteMessage, notification_id)); .setContentIntent(getNotificationIntent(remoteMessage, notification_id))
.setDeleteIntent(getDeleteIntent(remoteMessage));
/* 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);
......
package app.insti;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.ServiceGenerator;
public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.NOTIF_CANCELLED)) {
// Get the notification id
String id = intent.getExtras().getString(Constants.FCM_BUNDLE_NOTIFICATION_ID);
if (id == null || id.equals("")) return;
// Get retrofit and session id
ServiceGenerator serviceGenerator = new ServiceGenerator(context);
RetrofitInterface retrofitInterface = serviceGenerator.getRetrofitInterface();
SessionManager session = new SessionManager(context);
if (session.isLoggedIn()) {
Utils.setSessionId(session.getSessionID());
}
// Mark as read
retrofitInterface.markNotificationDeleted(Utils.getSessionIDHeader(), id).enqueue(new EmptyCallback<Void>());
}
}
}
...@@ -112,6 +112,9 @@ public interface RetrofitInterface { ...@@ -112,6 +112,9 @@ public interface RetrofitInterface {
@GET("notifications/read/{notificationID}") @GET("notifications/read/{notificationID}")
Call<Void> markNotificationRead(@Header("Cookie") String sessionID, @Path("notificationID") String notificationID); Call<Void> markNotificationRead(@Header("Cookie") String sessionID, @Path("notificationID") String notificationID);
@GET("notifications/read/{notificationID}?delete=1")
Call<Void> markNotificationDeleted(@Header("Cookie") String sessionID, @Path("notificationID") String notificationID);
@GET("logout") @GET("logout")
Call<Void> logout(@Header("Cookie") String sessionID); Call<Void> logout(@Header("Cookie") String sessionID);
......
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