Skip to content
Wu.XiaoFei edited this page May 19, 2016 · 10 revisions

Add maven dependency

<dependency>
    <groupId>com.github.teaey</groupId>
    <artifactId>apns4j</artifactId>
    <version>1.0.1</version>
</dependency>

Code examples

1 get a keystore

KeyStoreWraper keyStoreWrapper = 
KeyStoreHelper.getKeyStoreWraper("iphone_dev.p12", keyStorePasswd);

2 get apple server with env

AppleNotificationServer appleNotificationServer = 
AppleNotificationServer.get(AppleGateway.ENV_DEVELOPMENT);

3 init ssl socket factory

SecuritySocketFactory socketFactory = SecuritySocketFactory.
Builder.newBuilder().appleServer(appleNotificationServer).
keyStoreWrapper(keyStoreWrapper).build();

4 create a ssl socket

SecurityConnection connection = SecurityConnection.newSecurityConnection(socketFactory);
Assert.assertNotNull(connection);

5 create & init a notify payload

NotifyPayload notifyPayload = NotifyPayload.newNotifyPayload();
notifyPayload.alert("" + System.currentTimeMillis());
notifyPayload.badge(2);

//notifyPayload.sound("default");

//notifyPayload.alertBody("Pushed By \" apns4j");

//notifyPayload.alertActionLocKey("Button Text");

6 send

connection.sendAndFlush(deviceTokenString, notifyPayload);
connection.close(); 
Clone this wiki locally