Java 无效的十六进制字符:h

Java Invalid hex character: h

我正在使用 Java 8 与 Spring 和 Maven。

我正在尝试根据 following tutorial 设置推送通知。我让它们完美地用于 Android,使用以下内容:

NotificationServiceImpl.java

private String sendAndroidPushNotification(String device_token, String topics, String title, String message)
        throws Exception {
    String pushMessage = null;
    if (device_token != null && !device_token.equals("null")) {
        pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\":\""
                + device_token + "\"}";
    } else {
        pushMessage = "{\"data\":{\"title\":\"" + title + "\",\"message\":\"" + message + "\"},\"to\": \"/topics/"
                + topics + "\"}";
    }
    // Create connection to send FCM Message request.
    URL url = new URL("https://fcm.googleapis.com/fcm/send");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
    conn.setRequestProperty("Content-Type", "application/json");
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    // Send FCM message content.
    OutputStream outputStream = conn.getOutputStream();
    outputStream.write(pushMessage.getBytes());

    return "Android Push Notification: " + conn.getResponseCode() + " " + conn.getResponseMessage() + " - " + pushMessage;
}

但是,我在尝试为 iOS 设置时遇到问题。我收到以下错误:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
java.lang.RuntimeException: Invalid hex character: h
  at com.notnoop.apns.internal.Utilities.charval(Utilities.java:132)
  at com.notnoop.apns.internal.Utilities.decodeHex(Utilities.java:119)
  at com.notnoop.apns.EnhancedApnsNotification.<init>(EnhancedApnsNotification.java:77)
  at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:54)
  at com.notnoop.apns.internal.ApnsServiceImpl.push(ApnsServiceImpl.java:36)
  at com.notnoop.apns.internal.AbstractApnsService.push(AbstractApnsService.java:45)
  at com.jobs.spring.service.NotificationServiceImpl.sendIOSPushNotification(NotificationServiceImpl.java:90)

我的iOS代码如下:

/**
 * import com.notnoop.apns.APNS; import com.notnoop.apns.ApnsService;
 * https://github.com/notnoop/java-apns.
 */
private static String sendIOSPushNotification(String device_token, String topics, String title, String message)
        throws Exception {
    ApnsService service = APNS.newService().withCert(PATH_TO_P12_CERT, CERT_PASSWORD).withSandboxDestination()
            .build();

    String payload = APNS.newPayload()
            // .customFields(map)
            .alertBody(title + " " + message).sound("default").build();

    service.push(topics, payload); // <<<<< line 90
    return "iOS Push Notification: " + title + " " + message;
}

如您所见,该错误是第 90 行代码行的结果。

service.push(topics, payload);

我认为这是代码与tutorial不同的结果,如下所示:

service.push(DEVICE_TOKEN, payload);

我需要我的通知服务使用 topic 而不是 device token(客户端订阅 topic)。如上所示,这对 Android 成功。

问题

如果有人能阐明我如何解决这个 iOS 问题,我将不胜感激。

谢谢

更新

我会添加更多信息,因为从下面的评论(感谢您的帮助),我被告知参数应该是 devise token 而不是 topic

我的问题是我的客户订阅了一个主题。这是我无法更改的业务逻辑。

        let topics: string[] = [this.personModelLoggedIn.uid];
        const options: PushOptions = {
          android: {
            senderID: "XXXXXXXXXXXX",
            sound: "true",
            vibrate: "true",
            topics: topics
          },
          ios: {
            senderID: "XXXXXXXXXXXX",
            alert: "true",
            badge: true,
            sound: "true",
            topics: topics
          },
          windows: {}
        };

我看到 com.notnoop.apns.ApnsService 收到了 Collection<String> 个设计令牌。

push(Collection<String> deviceTokens, String payload)

我会尝试将我的 topic String 放入 Collection,看看是否可行。

如您在 src code 中所见:

58     /** 
59      * The infinite future for the purposes of Apple expiry date 
60      */ 
61     public final static int MAXIMUM_EXPIRY = Integer.MAX_VALUE; 
62 

63     /** 
64      * Constructs an instance of {@code ApnsNotification}. 
65      * 
66      * The message encodes the payload with a {@code UTF-8} encoding. 
67      * 
68      * @param dtoken    The Hex of the device token of the destination phone 
69      * @param payload   The payload message to be sent 
70      */ 
71     public EnhancedApnsNotification( 
72             int identifier, int expiryTime, 
73             String dtoken, String payload) { 
74         this.identifier = identifier; 
75         this.expiry = expiryTime; 
76         this.deviceToken = Utilities.decodeHex(dtoken); 
77         this.payload = Utilities.toUTF8Bytes(payload); 
78     } 

devicetoken 必须是十六进制编码。