APNS推送最新代码流程

不废话,直接来思路(亲测):


第一步导入jar包:(开源项目Pushy)

		
		    com.turo
		    pushy
		    0.10.2
		
		
		    org.eclipse.jetty.alpn
		    alpn-api
		    1.1.2.v20150522
		
		

细节点(花费3h调试):

如果tomcat里面含有tcnative,就不需要netty-tcnative,避免冲突;

如果tomcat里面没有tcnative, 就需要引入tcnative;


第二步代码演示:

			Future> responseFuture = null;
				try {
				// 创建ApnsClient
		        final ApnsClient apnsClient = new ApnsClientBuilder().setClientCredentials(new File(p12证书), p12密码).build();
		        // 沙箱环境
		        final Future connectFutrue = apnsClient.connect(ApnsClient.DEVELOPMENT_APNS_HOST);
		        // 正式环境
//		        final Future connectFutrue = apnsClient.connect(ApnsClient.PRODUCTION_APNS_HOST);        
		        connectFutrue.await();
		        final ApnsPayloadBuilder payBuilder = new ApnsPayloadBuilder();
		        // 通知信息
		        payBuilder.setAlertTitle("Title");
		        payBuilder.setAlertBody("AlertBody");
		        payBuilder.setSoundFileName(ApnsPayloadBuilder.DEFAULT_SOUND_FILENAME);
		        payBuilder.addCustomProperty("image", "https://onevcat.com/assets/images/background-cover.jpg");
		        payBuilder.setMutableContent(true);
		        payBuilder.setBadgeNumber(1);
		        // 通知的最大长度
		        String payload = payBuilder.buildWithDefaultMaximumLength();
							// 获取APNS指定的token
						final String token = TokenUtil.sanitizeTokenString(device.getDeviceToken());						
						SimpleApnsPushNotification simpleApnsPushNotification = new SimpleApnsPushNotification(token, device.getEdition().getBundleId(), payload);
						responseFuture = apnsClient.sendNotification(simpleApnsPushNotification);
						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都发送完毕并回复状态后关闭连接
				final Future disconnectFuture = apnsClient.disconnect();
				disconnectFuture.await();
			    return responseFuture;
				} catch (Exception e) {
		            if(e instanceof InterruptedException) {
		                System.out.println("Failed to disconnect APNs , timeout");
		            }
		            e.printStackTrace();
		        }
		}


更多文章,请关注:http://blog.csdn.net/qq_37022150




你可能感兴趣的:(推送)