2

In my app, I am doing multiple upload requests using retrofit 2 and launching a new notification for each upload. After going through a lot of similar questions, I am not sure how can I cancel the request using Call.cancel for a particular upload when its notification action cancel button clicked.

Thanks!

Sam Berg
  • 189
  • 3
  • 11
  • Each notification has an id and, you can control click cancel button by `BroadcastReceiver`? – RoShan Shan Mar 04 '17 at 03:52
  • @RoShanShan thanks for commenting, I think I didn't get your point. I know each notification has an id, but how can I cancel the retrofit request, after cancel is clicked? I think I'd need a `call` object for it. – Sam Berg Mar 04 '17 at 07:17

3 Answers3

3

Define a Map object

Map<int,Call> callStack= new HashMap<>();

When called to your_call_object add this.

int notificationId = ((Double) (Math.random() * 10000)).intValue()
callStack.put(notificationId, your_call_object)

When you creating a notification object add that notificationId.

notificationManager.notify(notificationId, notification.build());

When clicked Cancel Button, find that notificationId and...

Call your_call_object = callStack.getObject(notificaitonId);
your_call_object.cancel();
Kaloglu
  • 1,651
  • 1
  • 20
  • 31
-1

please read this link. a very detail explanation is provided tailor it to your needs.https://futurestud.io/tutorials/retrofit-2-cancel-requests

Remario
  • 3,813
  • 2
  • 18
  • 25
  • thanks for answering... actually I have gone through that tutorial but couldn't find my answer. My question is where can I put `call.cancel()` when my notification action is clicked? How should my pending intent look like? I need a reference to call object, right. – Sam Berg Mar 04 '17 at 03:34
-1

try to connect your service notification with your activity or service upload with a broadcast, in your notification action cancel send the broadcaster:

//declare in your manifest your broadcast class

Intent intentBroadcast = new Intent();
        intentBroadcast.setAction("CANCEL_RETROFIT_CALL");
        intentBroadcast.putExtra("ID_CALL","upload_123");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intentBroadcast);
        PendingIntent pendintAction = PendingIntent.getBroadcast(this, 0, intentBroadcast, PendingIntent.FLAG_UPDATE_CURRENT);

//then set the action setup

action = new NotificationCompat.Action.Builder(R.drawable.ic_action_cancel,
                        "cancel", pendintAction).build();
//launch your notification
NotificationCompat.Builder notificacion = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_noti_logo)
                    .setContentTitle(remoteMessage.getData().getAlert())
                    .setContentText(remoteMessage.getMessage())
                    .setAutoCancel(true)
                    .setSound(alarmSound)
                    .setContentIntent(pendingIntent)
                    .setVibrate(new long[]{1000, 1000})
                    .addAction(action);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificacion.build());

//in your actionbroadcast class

public class ActionBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        LocalBroadcastManager broadcaster = LocalBroadcastManager.getInstance(context);
        String accion = intent.getAction();
        String id_call = intent.getStringExtra("ID_CALL");


        if (accion.equals("CANCEL_RETROFIT_CALL")) {
            switch (id_call) {
                case callID:
                    intent = new Intent("CANCEL_RETROFIT_CALL");
                    intent.putExtra("id_call", id);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    break;

            }
        }
    }


}

i hope that this conde can help u .. https://github.com/googlesamples/android-UniversalMusicPlayer

Osvaldo Bringaz
  • 127
  • 1
  • 7
  • Hi, thanks for answering, actually my problem is that I can't figure out where to cancel the retrofit request using Call.cancel(). When i receive the broadcast receiver, how can i have a reference to my call object on which i need to call the cancel method? – Sam Berg Mar 04 '17 at 05:42