Skip to content

Instantly share code, notes, and snippets.

@secondsun
Created April 19, 2018 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secondsun/35db2bda4a05d5fa7d38c8f0234b8e68 to your computer and use it in GitHub Desktop.
Save secondsun/35db2bda4a05d5fa7d38c8f0234b8e68 to your computer and use it in GitHub Desktop.
private Request<Void> registerDevice(JSONObject data) {
nonNull(data, "data");
return Requester.call(() -> {
data.put("deviceToken", FirebaseInstanceId.getInstance().getToken());
String authHash = getHashedAuth(unifiedPushCredentials.getVariant(),
unifiedPushCredentials.getSecret().toCharArray());
final HttpRequest httpRequest = core.getHttpLayer().newRequest();
httpRequest.addHeader("Authorization", authHash);
// Invalidate old on Unified Push Server
String oldDeviceToken = retrieveOldDeviceToken();
if (oldDeviceToken != null) {
httpRequest.addHeader("x-ag-old-token", oldDeviceToken);
}
return httpRequest
.post(url + registryDeviceEndpoint, data.toString().getBytes());
}).map((MapFunction<Request<HttpResponse>, Request<Void>>) value -> value.map(httpResponse -> {
switch (httpResponse.getStatus()) {
case HTTP_OK:
FirebaseMessaging firebaseMessaging =
FirebaseMessaging.getInstance();
try {
JSONArray categories =
data.getJSONArray("categories");
for (int i = 0; i < categories.length(); i++) {
String category = categories.getJSONObject(i)
.toString();
firebaseMessaging.subscribeToTopic(category);
}
} catch (JSONException e) {
// ignore
}
firebaseMessaging.subscribeToTopic(
unifiedPushCredentials.getVariant());
saveCache(data);
return null;
default:
throw new HttpException(httpResponse.getStatus());
}
})).map(new MapFunction<Request<Void>, Void>() {
@Override
public Void map(Request<Void> value) {
return null;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment