Pushy初识

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

Introduction

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

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

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

maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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中添加如下配置:
1
2
3
4
5
6
7
8
9
10
11
<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
    证书错误,更换证书即可