如何在单击通知操作时取消改装2请求?

时间:2017-03-04 03:17:32

标签: android android-notifications retrofit2

在我的应用中,我正在使用改装2执行多个上传请求,并为每次上传启动新通知。 在经历了很多类似的问题后,我不确定如何在点击通知操作取消按钮时使用Call.cancel取消特定上传的请求。

谢谢!

3 个答案:

答案 0 :(得分:3)

定义地图对象

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

当调用 your_call_object 添加此内容时。

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

创建通知对象时添加该notificationId。

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

点击取消按钮后,找到notificationId和...

Call your_call_object = callStack.getObject(notificaitonId);
your_call_object.cancel();

答案 1 :(得分:-1)

请阅读此链接。根据您的需要提供非常详细的解释。https://futurestud.io/tutorials/retrofit-2-cancel-requests

答案 2 :(得分:-1)

尝试通过广告将您的服务通知与您的活动或服务上传相关联,在您的通知操作中取消发送给广播公司:

//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;

            }
        }
    }


}

我希望这个conde可以帮助你.. https://github.com/googlesamples/android-UniversalMusicPlayer