APNS推送最新代码流程

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


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

		<dependency>
		    <groupId>com.turo</groupId>
		    <artifactId>pushy</artifactId>
		    <version>0.10.2</version>
		</dependency>
		<dependency>
		    <groupId>org.eclipse.jetty.alpn</groupId>
		    <artifactId>alpn-api</artifactId>
		    <version>1.1.2.v20150522</version>
		</dependency>
<!-- 		<dependency>
		    <groupId>io.netty</groupId>
		    <artifactId>netty-tcnative-boringssl-static</artifactId>
		    <version>2.0.6.Final</version>
		</dependency>
 -->		<!-- 其他工具 -->

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

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

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


第二步代码演示:

			Future<PushNotificationResponse<SimpleApnsPushNotification>> responseFuture = null;
				try {
				// 创建ApnsClient
		        final ApnsClient apnsClient = new ApnsClientBuilder().setClientCredentials(new File(p12证书), p12密码).build();
		        // 沙箱环境
		        final Future<Void> connectFutrue = apnsClient.connect(ApnsClient.DEVELOPMENT_APNS_HOST);
		        // 正式环境
//		        final Future<Void> 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<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都发送完毕并回复状态后关闭连接
				final Future<Void> 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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值