APNs Pushy初识

作者: locoder | 来源:发表于2017-02-04 11:48 被阅读1439次

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

Introduction

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

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

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

maven依赖

<!-- pushy -->
<dependency>
   <groupId>com.relayrides</groupId>
   <artifactId>pushy</artifactId>
   <version>0.8.2</version>
</dependency>

<dependency>
   <groupId>io.netty</groupId>  
   <artifactId>netty-tcnative-boringssl-static</artifactId>
   <version>1.1.33.Fork24</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.eclipse.jetty.alpn/alpn-api -->
<dependency>
   <groupId>org.eclipse.jetty.alpn</groupId>
   <artifactId>alpn-api</artifactId>
   <version>1.1.2.v20150522</version>
</dependency>

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<Void> 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<PushNotificationResponse<SimpleApnsPushNotification>> responseFuture = apnsClient
                .sendNotification(notification);
        responseFuture
                .addListener(new GenericFutureListener<Future<PushNotificationResponse<SimpleApnsPushNotification>>>() {

                    @Override
                    public void operationComplete(Future<PushNotificationResponse<SimpleApnsPushNotification>> arg0)
                            throws Exception {
                        try {
                            final PushNotificationResponse<SimpleApnsPushNotification> 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<Void> 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中添加如下配置:
<dependency>
   <groupId>io.netty</groupId>
   <artifactId>netty-tcnative-boringssl-static</artifactId>
   <version>1.1.33.Fork24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty.alpn/alpn-api -->
<dependency>
   <groupId>org.eclipse.jetty.alpn</groupId>
   <artifactId>alpn-api</artifactId>
   <version>1.1.2.v20150522</version>
</dependency>
  • error:10000438:SSL routines:OPENSSL_internal:TLSV1_ALERT_INTERNAL_ERROR
    证书错误,更换证书即可

相关文章

  • APNs Pushy初识

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

  • Pushy入门文档中文翻译

    pushy 这是我自己的翻译版本,原文地址。 Pushy 是一个发送 APNs (iOS, OS X, 或 Saf...

  • 使用Pushy进行APNs消息推送

    最近对项目组的老的苹果IOS推送进行了升级修改。看了看苹果的接口文档,感觉自己直接来写一个保证稳定和高效的接口还是...

  • Redis 动态字符串 SDS 源码解析

    本文作者: Pushy本文链接: http://pushy.site/2019/12/21/redis-sds/版...

  • Android关于React-Native的react-nati

    pushy的补丁版本升级是基于pushy自动给原生分配的packageId[包版本序号], 精确到某个原生应用的某...

  • APNS与VoIP

    APNS 一、简述APNS APNS全称是Apple Push Notification service(苹果推送...

  • RN热更新react-native-pushy

    Pushy:http://update.reactnative.cn/home githubDemo:ReactN...

  • iOS远程推送(Objective-C & Swift)

    iOS远程推送 APNS远程推送的流程: 1、app 注册到 APNS。2、APNS 下发 devicetoken...

  • iOS APNS

    APNS推送机制 APNS注意事项 1、APNS免费,但需要开发者账号2、APNS不稳定,Apple对消息推送的可...

  • iOS的APNs

    Communicating with APNs The APNs provider API lets you se...

网友评论

    本文标题:APNs Pushy初识

    本文链接:https://www.haomeiwen.com/subject/bgmjittx.html