Azure/azure-notificationhubs-java-backend

Direct_send method is not there in the SDK

PrinceVarshney opened this issue · 2 comments

According to the rest APIs there is a concept of direct send which takes the pns credentials in the header to send the push notification directly onto the specific device but right now no such method exists. In unofficial version of the API, method was there but now it is being removed.

I think it is there:

public NotificationOutcome sendDirectNotification(Notification notification, String deviceHandle) throws NotificationHubsException {
SyncCallback<NotificationOutcome> callback = new SyncCallback<NotificationOutcome>();
sendDirectNotificationAsync(notification, deviceHandle, callback);
return callback.getResult();
}
@Override
public NotificationOutcome sendDirectNotification(Notification notification, List<String> deviceHandles) throws NotificationHubsException {
SyncCallback<NotificationOutcome> callback = new SyncCallback<NotificationOutcome>();
sendDirectNotificationAsync(notification, deviceHandles, callback);
return callback.getResult();
}
@Override
public void sendDirectNotificationAsync(Notification notification,
String deviceHandle, final FutureCallback<NotificationOutcome> callback) {
try {
URI uri = new URI(endpoint + hubPath + "/messages" + APIVERSION + "&direct");
final HttpPost post = new HttpPost(uri);
final String trackingId = java.util.UUID.randomUUID().toString();
post.setHeader("ServiceBusNotification-DeviceHandle", deviceHandle);
post.setHeader("Authorization", generateSasToken(uri));
post.setHeader(TRACKING_ID_HEADER, trackingId);
for (String header : notification.getHeaders().keySet()) {
post.setHeader(header, notification.getHeaders().get(header));
}
post.setEntity(new StringEntity(notification.getBody(), notification.getContentType()));
HttpClientManager.getHttpAsyncClient().execute(post, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
try{
int httpStatusCode = response.getStatusLine().getStatusCode();
if (httpStatusCode != 201) {
String msg = "";
if (response.getEntity() != null&& response.getEntity().getContent() != null) {
msg = IOUtils.toString(response.getEntity().getContent());
}
callback.failed(new NotificationHubsException("Error: " + response.getStatusLine() + " body: " + msg, httpStatusCode));
return;
}
String notificationId = null;
Header locationHeader = response.getFirstHeader(CONTENT_LOCATION_HEADER);
if(locationHeader != null){
URI location = new URI(locationHeader.getValue());
String[] segments = location.getPath().split("/");
notificationId = segments[segments.length-1];
}
callback.completed(new NotificationOutcome(trackingId, notificationId));
} catch (Exception e) {
callback.failed(e);
} finally {
post.releaseConnection();
}
}
public void failed(final Exception ex) {
post.releaseConnection();
callback.failed(ex);
}
public void cancelled() {
post.releaseConnection();
callback.cancelled();
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void sendDirectNotificationAsync(Notification notification, List<String> deviceHandles, final FutureCallback<NotificationOutcome> callback) {
try {
URI uri = new URI(endpoint + hubPath + "/messages/$batch" + APIVERSION + "&direct");
final HttpPost post = new HttpPost(uri);
final String trackingId = java.util.UUID.randomUUID().toString();
post.setHeader("Authorization", generateSasToken(uri));
post.setHeader(TRACKING_ID_HEADER, trackingId);
for (String header : notification.getHeaders().keySet()) {
post.setHeader(header, notification.getHeaders().get(header));
}
FormBodyPart notificationPart = FormBodyPartBuilder.create()
.setName("notification")
.addField("Content-Disposition", "inline; name=notification")
.setBody(new StringBody(notification.getBody(), notification.getContentType()))
.build();
String deviceHandlesJson = new GsonBuilder().disableHtmlEscaping().create().toJson(deviceHandles);
FormBodyPart devicesPart = FormBodyPartBuilder.create()
.setName("devices")
.addField("Content-Disposition", "inline; name=devices")
.setBody(new StringBody(deviceHandlesJson, ContentType.APPLICATION_JSON))
.build();
HttpEntity entity = MultipartEntityBuilder.create()
.addPart(notificationPart)
.addPart(devicesPart)
.build();
post.setEntity(entity);
HttpClientManager.getHttpAsyncClient().execute(post, new FutureCallback<HttpResponse>() {
public void completed(final HttpResponse response) {
try{
int httpStatusCode = response.getStatusLine().getStatusCode();
if (httpStatusCode != 201) {
String msg = "";
if (response.getEntity() != null&& response.getEntity().getContent() != null) {
msg = IOUtils.toString(response.getEntity().getContent());
}
callback.failed(new NotificationHubsException("Error: " + response.getStatusLine() + " body: " + msg, httpStatusCode));
return;
}
String notificationId = null;
Header locationHeader = response.getFirstHeader(CONTENT_LOCATION_HEADER);
if(locationHeader != null){
URI location = new URI(locationHeader.getValue());
String[] segments = location.getPath().split("/");
notificationId = segments[segments.length-1];
}
callback.completed(new NotificationOutcome(trackingId, notificationId));
} catch (Exception e) {
callback.failed(e);
} finally {
post.releaseConnection();
}
}
public void failed(final Exception ex) {
post.releaseConnection();
callback.failed(ex);
}
public void cancelled() {
post.releaseConnection();
callback.cancelled();
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}

It was added 30 Dec 2016: 50e718d

so 11 days after you opened the issue...

Would you mind closing this issue?

(I'm not the maintainer, just passing by)

Closing as per request