Coder Social home page Coder Social logo

Comments (2)

doppelganger9 avatar doppelganger9 commented on September 26, 2024

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)

from azure-notificationhubs-java-backend.

mpodwysocki avatar mpodwysocki commented on September 26, 2024

Closing as per request

from azure-notificationhubs-java-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.