Created
April 19, 2018 19:23
-
-
Save secondsun/35db2bda4a05d5fa7d38c8f0234b8e68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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