黄鹏宇
发布于 2024-09-07 / 6 阅读
0
0

apns

服务端

public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
    final ApnsClient apnsClient = new ApnsClientBuilder().setApnsServer(ApnsClientBuilder.DEVELOPMENT_APNS_HOST).setClientCredentials(new File("/Users/uuorb/Desktop/allinpanel/cert.p12"),"HPyuko12")
            .build();
    Map<String, Object> aps = new HashMap<String, Object>(1);
    aps.put("alert", "推送内容");
    aps.put("sound", "default");
    aps.put("badge", 1);

    Map<String, Object> messagePayload = new HashMap<String, Object>(1);
    messagePayload.put("aps", aps);
    String payload = JSONObject.toJSONString(messagePayload);
    String token = "b58159c73087a9e95f4ee46ec050dd9f58e2b2e53f8d970832a823e9ffe652e8";
    SimpleApnsPushNotification pushNotification = new SimpleApnsPushNotification(token, "com.uuorb.allinpanel", payload);

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

    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());

      if (pushNotificationResponse.getTokenInvalidationTimestamp().isPresent()) {
        System.out.println("\t…and the token is invalid as of " +
                pushNotificationResponse.getTokenInvalidationTimestamp());
      }
    }

  }

评论