gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:45:13 25 4
gpt4 key购买 nike

我正在将 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>设计 token 。

push(Collection<String> deviceTokens, String payload)

我会尝试把我的topic StringCollection ,看看是否有效。

最佳答案

正如您在 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 }

设备 token 必须是十六进制编码。

关于Java 无效的十六进制字符 : h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44691925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com