EOEditingContext ec = newEditingContext();
ec.lock();
// convert global ids back into eos in the new ec
notification = (Notification)ec.faultForGlobalID(notificationID, ec);
devicesToNotify = ERXEOControlUtilities.faultsForGlobalIDs(ec, deviceIds);
String complex = APNS.newPayload()
.actionKey("Details\u2026")
.localizedKey(notification.notificationType().typeName())
.localizedArguments(shortString(notification.textContents()))
.badge(1)//.sound("default")
.customField("id", notification.notificationIdentifier())
.build();
String certificatePath = APNsUtilities.certBase + notification.application().certificateOrAndroidc2dmUser();
NSTimestamp notificationExpiry = notification.createDate().timestampByAddingGregorianUnits(
0,
0,
0,
0,
((notification.minutesToExpire() != null) ? notification.minutesToExpire() : notification.notificationType().defaultMinutesToExpire()),
0);
System.out.println("\ncertificatePath is: " + certificatePath);
if (!(new File(certificatePath).exists())) {
System.out.println("Certificate not found at path: " + certificatePath);
// return false;
}
ApnsServiceBuilder sb = APNS.newService()
.withCert(certificatePath, notification.application().certificatePassword());
sb.withDelegate(APNsDelegate.getSingletonObject());
boolean sandbox = APNsUtilities.HOST.contains("sandbox");
if(sandbox) System.out.println("Sandbox host selected");
else {
System.out.println("Production host selected");
}
ApnsService service = APNsUtilities.HOST.contains("sandbox") ?
sb.withSandboxDestination().build() :
sb.withProductionDestination().build();
System.out.println("push should be: " + complex);
System.out.println("Should push to: " + devicesToNotify.count() + " devices");
int deviceCount = devicesToNotify.count();
int receivers = 0;
try {
for(ApplicationDevice deviceToNotify : devicesToNotify){
try {
String client = deviceToNotify.deviceToken();
if(!(client == null || client.equals("") || client.equals("0"))) {
service.push(client, complex, notificationExpiry);//(client, complex); // can add additional argument here for expiry
if(sandbox) System.out.println("In sandbox, pushed to device with udid " + deviceToNotify.udid() + " and token " + deviceToNotify.deviceToken() + " pushed to Apple");
receivers++;
}
ApplicationDeviceNotification appDevNote =
ApplicationDeviceNotification.createApplicationDeviceNotification(notification.editingContext(), deviceToNotify, notification);
} catch (Exception e) {
System.out.println("\nPush for Notification id " + notification.notificationIdentifier() + " failed for device with token " + deviceToNotify.deviceToken() + "\nand udid " + deviceToNotify.udid());
e.printStackTrace();
}
}
System.out.println("Device expecting push = " + deviceCount + " and devices receiving the push = " + receivers);
service.stop();
ec.saveChanges();
} catch (Exception e) {
e.printStackTrace();
} finally {
ec.unlock();