|
You send push notifications to clients in the same way that iOS and OS X apps push notifications to APNs. As a push notification provider, you communicate with APNs over a binary interface. This a high-speed, high-capacity interface uses a streaming TCP socket design with binary content. The binary interface is asynchronous.

The binary interface of the production environment is available through gateway.push.apple.com, port 2195. Do not connect to the development environment to send Safari Push Notifications. You may establish multiple parallel connections to the same gateway or to multiple gateway instances.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import javax.net.ssl.SSLException;
import com.relayrides.pushy.apns.ApnsClient;
import com.relayrides.pushy.apns.ApnsClientBuilder;
import com.relayrides.pushy.apns.ClientNotConnectedException;
import com.relayrides.pushy.apns.DeliveryPriority;
import com.relayrides.pushy.apns.PushNotificationResponse;
import com.relayrides.pushy.apns.util.ApnsPayloadBuilder;
import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;
import com.relayrides.pushy.apns.util.TokenUtil;
import io.netty.util.concurrent.Future;
public class TestSafariPush {
public static String p12path = "/tmp/WebPushDist.p12";
public static String p12pass = "123456";
private String getTopicName(String p12Path, String p12Pass) throws Exception {
KeyStore p12 = KeyStore.getInstance("pkcs12");
p12.load(new FileInputStream(p12Path), p12Pass.toCharArray());
Enumeration e = p12.aliases();
Map<String,String> subjectMap = new HashMap<String,String>();
while (e.hasMoreElements()) {
String alias = (String) e.nextElement();
X509Certificate c = (X509Certificate) p12.getCertificate(alias);
Principal subject = c.getSubjectDN();
String subjectArray[] = subject.toString().split(",");
for (String s : subjectArray) {
String[] str = s.trim().split("=");
subjectMap.put(str[0], str[1]);
}
}
return subjectMap.get("UID");
}
private Date getInvalidationDatetime(int ttl){
long ttlmillisecs = System.currentTimeMillis() + (ttl * 1000);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(ttlmillisecs);
return calendar.getTime();
}
private ApnsClient getClient() throws SSLException, IOException, InterruptedException{
final ApnsClient apnsClient = new ApnsClientBuilder().setClientCredentials(new File(p12path), p12pass).build();
final Future<Void> connectFuture = apnsClient.connect(ApnsClient.PRODUCTION_APNS_HOST);
connectFuture.await();
return apnsClient;
}
public void sendSingleMessage(String title, String message, int ttl, String collapseKey, String topic, String token) throws IOException, InterruptedException {
try {
ApnsClient apnsClient = getClient();
final ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();
payloadBuilder.setAlertBody(message);
payloadBuilder.setAlertTitle(title);
payloadBuilder.setActionButtonLabel("view");
final String payload = payloadBuilder.buildWithDefaultMaximumLength();
token = TokenUtil.sanitizeTokenString(token);
Date invalidationTime = getInvalidationDatetime(ttl);
final SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(token, topic, payload, invalidationTime, DeliveryPriority.IMMEDIATE, collapseKey);
final Future<PushNotificationResponse<SimpleApnsPushNotification>> sendNotificationFuture = apnsClient.sendNotification(pushNotification);
try {
final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse = sendNotificationFuture.get();
if (pushNotificationResponse.isAccepted()) {
System.out.println("APNS Send SingleMessage Success : ");
} else {
System.out.println("APNS Send SingleMessage Failed : " + pushNotificationResponse.getRejectionReason() + " : ");
if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
System.out.println("Token is invalid as of "+ pushNotificationResponse.getTokenInvalidationTimestamp());
}
}
} catch (final ExecutionException e) {
if (e.getCause() instanceof ClientNotConnectedException) {
System.out.println("Waiting for client to reconnect…");
apnsClient.getReconnectionFuture().await();
System.out.println("Reconnected.");
}
}
} catch (Exception e) {
System.out.println("Some Error");
}
}
public static void main(String args[]){
TestSafariPush test = new TestSafariPush();
String token = "4B304B43A406DDBA912CAB3EE9982478793C4D51CC8AADF289BFCD5D0AFE89F3";
String topic;
try {
topic = test.getTopicName(p12path, p12pass);
test.sendSingleMessage("Hi Jai", "Make It Happen", 100, "1", topic, token);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}--
Pushy is an open-source Java library for sending APNs (iOS and OS X) push notifications. Pushy is brought to you by the engineers at RelayRides.
---
You received this message because you are subscribed to the Google Groups "pushy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pushy-apns+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to pushy-apns+...@googlegroups.com.