IOS推送-pushy

iOS

  • 引入jar包
    • 创建APNSConnect
    • 进行发送
    • 报错对照表

引入jar包

IOS推送-pushy_第1张图片

创建APNSConnect

创建APNSConnect,与APNs进行链接
public class APNSConnect {

private static ApnsClient apnsClient = null;

public static ApnsClient getAPNSConnectP8(String path,String teamId,String ketId) {

    if (apnsClient == null) {
        try {
            apnsClient = new ApnsClientBuilder()
                    .setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
                    .setSigningKey(ApnsSigningKey.loadFromPkcs8File(new File(path),
                            teamId, ketId))
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return apnsClient;

}

public static ApnsClient getAPNSConnectP12(String path,String password) {

    if (apnsClient == null) {
        try {
            EventLoopGroup eventLoopGroup = new NioEventLoopGroup(4);
            apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST)
                    .setClientCredentials(new File(path), password)
                    .setConcurrentConnections(4).setEventLoopGroup(eventLoopGroup).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return apnsClient;

}

}

进行发送

deviceToken为发送对象
alertTitle为发送标题
alertBody为发送内容
topic为包路径

public static void push(final String deviceToken, String alertTitle, String alertBody,boolean contentAvailable,Map<String, Object> customProperty,int badge,String topic) {

        // 包路径
        String path = "/app_push/IOS/3.4.2-dev.p8";

        // P8的teamId
        String teamId = "111111";

        // P8的keyId
        String keyId = "111111";

        // P12的证书密码
        String password = "111111";

        ApnsClient apnsClient = APNSConnect.getAPNSConnectP8(path,teamId,keyId);

        final AtomicLong successCnt = new AtomicLong(0);//线程安全的计数器

        ApnsPayloadBuilder payloadBuilder = new ApnsPayloadBuilder();

        if(alertBody!=null&&alertTitle!=null) {
            payloadBuilder.setAlertBody(alertBody);
            payloadBuilder.setAlertTitle(alertTitle);
        }

        //如果badge小于0,则不推送这个右上角的角标,主要用于消息盒子新增或者已读时,更新此状态
        if(badge>0) {
            payloadBuilder.setBadgeNumber(badge);
        }

        //将所有的附加参数全部放进去
        if(customProperty!=null) {
            for(Map.Entry<String, Object> map:customProperty.entrySet()) {
                payloadBuilder.addCustomProperty(map.getKey(),map.getValue());
            }
        }
    payloadBuilder.setContentAvailable(contentAvailable);
    // 通知的最大长度
    String payload = payloadBuilder.buildWithDefaultMaximumLength();
    final String token = TokenUtil.sanitizeTokenString(deviceToken);
    SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(token, topic, payload);

    final Future> future = apnsClient.sendNotification(pushNotification);

    future.addListener(new GenericFutureListener>() {
        public void operationComplete(Future pushNotificationResponseFuture) throws Exception {
            if (future.isSuccess()) {
                final PushNotificationResponse response = future.getNow();
                if (response.isAccepted()) {
                    System.out.println("发送成功");
                    successCnt.incrementAndGet();
                } else {
                    Date invalidTime = response.getTokenInvalidationTimestamp();
                    System.out.println("Notification rejected by the APNs gateway: " + response.getRejectionReason());
                    if (invalidTime != null) {
                        System.out.println("\t…and the token is invalid as of " + response.getTokenInvalidationTimestamp());
                    }
                }
            } else {
                // logger.error("send notification device token={} is failed {} ", token, future.cause().getMessage());
                System.out.println("send notification device token={"+ token +"} is failed {"+ future.cause().getMessage() +"} ");
            }
        }
    });
}

报错对照表

https://www.jianshu.com/p/f4f016848a4e

你可能感兴趣的:(ios,java)