如何将用户上下文对象传递给异步Jetty HTTP客户端的响应回调? [英] How to pass user context object to the response callback of async Jetty HTTP client?

查看:149
本文介绍了如何将用户上下文对象传递给异步Jetty HTTP客户端的响应回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 Google Firebase云消息传递

向单个收件人发送通知时, (code> 200 +错误:MissingRegistration , 200 +错误:InvalidRegistration 200 + error:NotRegistered ),这需要删除该收件人的标记(因为她例如重新安装了Android应用程序并且令牌已更改)。



我的问题是:

如何将该字符串(FCM令牌)传递回非阻塞的 Jetty HTTP客户端



目前我的解决方法是为我的请求添加一个自定义HTTP头:

  X-token:APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx。 .. 

然后我在响应cal中检索它lback。但这是一个诡计,因为FCM没有指定这样的头文件,而且我还需要传递更多的自定义数据(内部用户ID)。

my当前源代码与自定义HTTP头,如何改变它?

  private static final String FCM_URL = https://fcm.googleapis.com/fcm/send; 
private static final String FCM_KEY =key = REPLACE_BY_YOUR_KEY;
private static final String FCM_RESULTS =results;
private static final String FCM_ERROR =error;
private static final String FCM_NOT_REGISTERED =NotRegistered;
private static final String FCM_MISSING_REGISTRATION =MissingRegistration;
private static final String FCM_INVALID_REGISTRATION =InvalidRegistration;

private static final String FCM_X_TOKEN =X-token;
private static final String TOKEN =APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx ...;

private static final Map< String,Object> REQUEST = new HashMap<>();
private static final Map< String,Object> NOTIFICATION = new HashMap<>();
private static final Map< String,Object> DATA = new HashMap<>();

static {
REQUEST.put(to,TOKEN);
REQUEST.put(通知,NOTIFICATION);
REQUEST.put(data,DATA);
NOTIFICATION.put(body,great match!);
NOTIFICATION.put(title,葡萄牙对丹麦);
NOTIFICATION.put(icon,myicon);
DATA.put(Nick,Mario);
DATA.put(房间,PortugalVSDenmark);
}

private static final SslContextFactory sFactory = new SslContextFactory();
private static final HttpClient sHttpClient = new HttpClient(sFactory);
private static final BufferingResponseListener sFcmListener = new BufferingResponseListener(){
@Override $ b $ public void onComplete(Result result){$ b $ if if(!result.isSucceeded()){
System.err.println(result.getFailure());
return;
}

String body = getContentAsString(StandardCharsets.UTF_8);

尝试{
地图< String,Object> resp =(Map< String,Object>)JSON.parse(body);
Object [] results =(Object [])resp.get(FCM_RESULTS);
Map map =(Map)results [0];
String error =(String)map.get(FCM_ERROR);
System.out.printf(error:%s\\\
,error);
if(FCM_NOT_REGISTERED.equals(error)||
FCM_MISSING_REGISTRATION.equals(error)||
FCM_INVALID_REGISTRATION.equals(error)){
String token = result.getRequest() 。.getHeaders()得到(FCM_X_TOKEN);
System.out.printf(TODO从数据库中删除无效的FCM令牌:%s \ n,令牌);
}
} catch(Exception ex){
System.err.println(ex);
}
}
};

public static void main(String [] args)throws Exception {
sHttpClient.start();

.header(HttpHeader.CONTENT_TYPE,application / json)
.header(FCM_X_TOKEN,TOKEN) )//解决方法,如何改进?
.content(new StringContentProvider(JSON.toString(REQUEST)))
.send(sFcmListener);


解决方案

作为请求属性并将其恢复:
$ b

  httpClient。 POST(url)
.attribute(key,token)
...
.send(new BufferingResponseListener(){
@Override $ b $ public void onComplete(Result result ){
Object token = result.getRequest()。getAttribute(key);
...
}
});


When sending notifications to single recipients over Google Firebase Cloud Messaging, sometimes a response comes back (200 + error:MissingRegistration, 200 + error:InvalidRegistration, 200 + error:NotRegistered), which requires deleting the token of that recipient (because she for example reinstalled the Android app and the token has changed).

My question is:

How to pass that string (the FCM token) back to the response callback of the non-blocking Jetty HTTP client?

Currently my workaround is to add a custom HTTP header to my request:

X-token: APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...

and then I retrieve it in the response callback. But this is a hack, because FCM does not specify such a header and also I need to pass more custom data (the internal user id in my app) back.

Here is my current source code with the custom HTTP header, how to change it please?

private static final String FCM_URL                  = "https://fcm.googleapis.com/fcm/send";
private static final String FCM_KEY                  = "key=REPLACE_BY_YOUR_KEY";
private static final String FCM_RESULTS              = "results";
private static final String FCM_ERROR                = "error";
private static final String FCM_NOT_REGISTERED       = "NotRegistered";
private static final String FCM_MISSING_REGISTRATION = "MissingRegistration";
private static final String FCM_INVALID_REGISTRATION = "InvalidRegistration";

private static final String FCM_X_TOKEN              = "X-token";
private static final String TOKEN                    = "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...";

private static final Map<String, Object> REQUEST      = new HashMap<>();
private static final Map<String, Object> NOTIFICATION = new HashMap<>();
private static final Map<String, Object> DATA         = new HashMap<>();

static {
    REQUEST.put("to", TOKEN);
    REQUEST.put("notification", NOTIFICATION);
    REQUEST.put("data", DATA);
    NOTIFICATION.put("body", "great match!");
    NOTIFICATION.put("title", "Portugal vs. Denmark");
    NOTIFICATION.put("icon", "myicon");
    DATA.put("Nick", "Mario");
    DATA.put("Room", "PortugalVSDenmark");
}

private static final SslContextFactory sFactory = new SslContextFactory();
private static final HttpClient sHttpClient = new HttpClient(sFactory);
private static final BufferingResponseListener sFcmListener = new BufferingResponseListener() {
    @Override
    public void onComplete(Result result) {
        if (!result.isSucceeded()) {
            System.err.println(result.getFailure());
            return;
        }

        String body = getContentAsString(StandardCharsets.UTF_8);

        try {
            Map<String, Object> resp = (Map<String, Object>) JSON.parse(body);
            Object[] results = (Object[]) resp.get(FCM_RESULTS);
            Map map = (Map) results[0];
            String error = (String) map.get(FCM_ERROR);
            System.out.printf("error: %s\n", error);
            if (FCM_NOT_REGISTERED.equals(error) ||
                FCM_MISSING_REGISTRATION.equals(error) ||
                FCM_INVALID_REGISTRATION.equals(error)) {
                String token = result.getRequest().getHeaders().get(FCM_X_TOKEN);
                System.out.printf("TODO delete invalid FCM token from the database: %s\n", token);
            }
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
};

public static void main(String[] args) throws Exception {
    sHttpClient.start();
    sHttpClient.POST(FCM_URL)
        .header(HttpHeader.AUTHORIZATION, FCM_KEY)
        .header(HttpHeader.CONTENT_TYPE, "application/json")
        .header(FCM_X_TOKEN, TOKEN) // Workaround, how to improve?
        .content(new StringContentProvider(JSON.toString(REQUEST)))
        .send(sFcmListener);
}

解决方案

You want to set the token as a request attribute and the retrieve it back:

httpClient.POST(url)
        .attribute(key, token)
        ...
        .send(new BufferingResponseListener() {
            @Override
            public void onComplete(Result result) {
                Object token = result.getRequest().getAttribute(key);
                ...
            }
        });

这篇关于如何将用户上下文对象传递给异步Jetty HTTP客户端的响应回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆