APNs Pushy初识

Pushy是基于HTTP/2的Java类库,是向Apple的APNs发送推送通知的第三方类库。

Introduction

Pushy是一个给Apns的发推送通知的Java类库;是Turo创建和维护的项目。

与notnoop使用基于二进制协议不同的是,Pushy使用的是基于HTTP/2-based APNs 协议

具体两种协议的比较可参考:基于HTTP2的全新APNs协议

maven依赖



   com.relayrides
   pushy
   0.8.2



   io.netty  
   netty-tcnative-boringssl-static
   1.1.33.Fork24




   org.eclipse.jetty.alpn
   alpn-api
   1.1.2.v20150522

Example

作为一个程序员,果断上代码:

import java.io.File;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.relayrides.pushy.apns.ApnsClient;
import com.relayrides.pushy.apns.ApnsClientBuilder;
import com.relayrides.pushy.apns.ClientNotConnectedException;
import com.relayrides.pushy.apns.PushNotificationResponse;
import com.relayrides.pushy.apns.util.ApnsPayloadBuilder;
import com.relayrides.pushy.apns.util.SimpleApnsPushNotification;

import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;

public class PushyExample {

    public static void main(String[] args) throws Exception {
        final ApnsClient apnsClient = new ApnsClientBuilder()
                .setClientCredentials(new File("p12Path"), "111111").build();
        final Future connectFutrue = apnsClient.connect(ApnsClient.DEVELOPMENT_APNS_HOST);
        // 等待连接apns成功, 良好的编程习惯,需要有最长等待时间
        try {
            connectFutrue.await(10 , TimeUnit.MINUTES);
        } catch (Exception e) {
            if(e instanceof InterruptedException) {
                System.out.println("Failed to connect APNs , timeout");
            }
            e.printStackTrace();
        }
        final ApnsPayloadBuilder payBuilder = new ApnsPayloadBuilder();
        payBuilder.setAlertBody("pushy Example");
        String payload = payBuilder.buildWithDefaultMaximumLength();
        final String token = "******";
        SimpleApnsPushNotification notification = new SimpleApnsPushNotification(token, null, payload);
        Future> responseFuture = apnsClient
                .sendNotification(notification);
        responseFuture
                .addListener(new GenericFutureListener>>() {

                    @Override
                    public void operationComplete(Future> arg0)
                            throws Exception {
                        try {
                            final PushNotificationResponse pushNotificationResponse = arg0
                                    .get();

                            if (pushNotificationResponse.isAccepted()) {
                                System.out.println("Push notification accepted by APNs gateway.");
                            } else {
                                System.out.println("Notification rejected by the APNs gateway: "
                                        + pushNotificationResponse.getRejectionReason());

                                if (pushNotificationResponse.getTokenInvalidationTimestamp() != null) {
                                    System.out.println("\t…and the token is invalid as of "
                                            + pushNotificationResponse.getTokenInvalidationTimestamp());
                                }
                            }
                        } catch (final ExecutionException e) {
                            System.err.println("Failed to send push notification.");
                            e.printStackTrace();

                            if (e.getCause() instanceof ClientNotConnectedException) {
                                System.out.println("Waiting for client to reconnect…");
                                apnsClient.getReconnectionFuture().await();
                                System.out.println("Reconnected.");
                            }
                        }
                    }
                });
        // 结束后关闭连接, 该操作会直到所有notification都发送完毕并回复状态后关闭连接
        Future disconnectFuture = apnsClient.disconnect();
        try {
            disconnectFuture.await(1 , TimeUnit.HOURS);
        } catch (Exception e) {
            if(e instanceof InterruptedException) {
                System.out.println("Failed to disconnect APNs , timeout");
            }
            e.printStackTrace();
        }
        
    }
}

Error Lists

  • java.lang.NoClassDefFoundError: org/eclipse/jetty/alpn/ALPN$Provider
    Maven中添加如下配置:

   io.netty
   netty-tcnative-boringssl-static
   1.1.33.Fork24



   org.eclipse.jetty.alpn
   alpn-api
   1.1.2.v20150522

  • error:10000438:SSL routines:OPENSSL_internal:TLSV1_ALERT_INTERNAL_ERROR
    证书错误,更换证书即可

你可能感兴趣的:(APNs Pushy初识)