其他分享
首页 > 其他分享> > ios apn push - pushy测试

ios apn push - pushy测试

作者:互联网

参照文档

https://github.com/jchambers/pushy

按照上面的步骤一步步来.

首先添加pom依赖,刷新maven下载对应的包体;

向公司的管理这索取当前用来测试的.p12文件和对应的密码,和申请时的topic,填到代码中就可以了.

特别注意topic和密码和p12文件都对.

然后去对应的用户中找合适token用户测试正对这个用户的push,看到苹果反馈accepted就可以了.

 

详细代码如下:


    public static void main(String[] argv) throws Exception {
        
        final ApnsClient apnsClient = new ApnsClientBuilder()
                .setApnsServer(ApnsClientBuilder.PRODUCTION_APNS_HOST)
                .setClientCredentials(new File("D:/codes/Codes/DoomsdayServer1.0/conf/XXXXX.p12"), "xxxxx")
                .build();

        final SimpleApnsPushNotification pushNotification;

        {
            final ApnsPayloadBuilder payloadBuilder = new SimpleApnsPayloadBuilder();
            payloadBuilder.setAlertBody("Example!~~~");
            payloadBuilder.setAlertTitle("ex");
            payloadBuilder.setLocalizedAlertTitle("Open App");


            final String payload = payloadBuilder.build();
            final String token = TokenUtil.sanitizeTokenString("xxxxxx");

            pushNotification = new SimpleApnsPushNotification(token, "com.abc.def", payload);
        }


        final PushNotificationFuture<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>>
                sendNotificationFuture = apnsClient.sendNotification(pushNotification);


        try {
            final PushNotificationResponse<SimpleApnsPushNotification> pushNotificationResponse =
                    sendNotificationFuture.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());

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

        try {
            final CompletableFuture<Void> closeFuture = apnsClient.close();
        } catch (final Exception e) {
            System.err.println("Failed to send push notification.");
            e.printStackTrace();
        }
    }

 

标签:pushNotificationResponse,pushy,System,println,token,apn,payloadBuilder,push,fina
来源: https://blog.csdn.net/achen0001/article/details/117364874