Java无效的十六进制字符:h
我将Java8与Spring和Maven一起使用 我正在尝试基于设置推送通知。我让它们在安卓上完美运行,使用以下方法: NotificationServiceImpl.javaJava无效的十六进制字符:h,java,apple-push-notifications,Java,Apple Push Notifications,我将Java8与Spring和Maven一起使用 我正在尝试基于设置推送通知。我让它们在安卓上完美运行,使用以下方法: NotificationServiceImpl.java private String sendAndroidPushNotification(String device_token, String topics, String title, String message) throws Exception { String pushMessage =
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设置时遇到了一个问题。我得到以下错误:
我认为这是代码不同于的结果,如下所示:
/**
* 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;
}
service.push(DEVICE_TOKEN, payload);
我需要通知服务使用主题
,而不是设备令牌
(客户端订阅主题
)。正如您在上面所看到的,这在Android上成功运行
问题
如果有人能告诉我如何解决iOS的问题,我将不胜感激
多谢各位
更新
我将添加更多信息,因为从下面的评论(感谢帮助),我被告知参数应该是设计令牌
,而不是主题
我的问题是我的客户订阅了一个主题。这是我无法改变的商业逻辑
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: {}
};
我看到他们收到了一批设计代币
push(Collection<String> deviceTokens, String payload)
push(收集设备令牌、字符串有效负载)
我将尝试将我的主题
字符串
放入一个集合
,看看是否有效。如您所见:
devicetoken必须采用十六进制编码。正如您在以下列表中看到的:
devicetoken必须是十六进制编码。为什么投票失败?这不是一个合理的问题吗?你试过调试吗?“payload”变量中保存了什么?无效的十六进制字符:h我想说您必须发送十六进制值,但您发送了一个字符串我没有进行向下投票,但我们不知道参数是什么,异常情况似乎非常清楚。看来你的调试工有份工作为什么否决票?这不是一个合理的问题吗?你试过调试吗?“payload”变量中保存了什么?无效的十六进制字符:h我想说您必须发送十六进制值,但您发送了一个字符串我没有进行向下投票,但我们不知道参数是什么,异常情况似乎非常清楚。看来你的调试师有份工作谢谢你帮我解决了这个问题。我把话题转换成十六进制
publicstringtohex(stringarg){returnstring.format(“%040x”,新的biginger(1,arg.getBytes(/*YOUR_CHARSET?*/));}
@Richard我认为您最好使用类com.notnoop.apns.internal.Utilities
service.push(Utilities.encodeHex(topics.getBytes()),payload)中的方法代码>谢谢,这为我解决了问题。我把话题转换成十六进制publicstringtohex(stringarg){returnstring.format(“%040x”,新的biginger(1,arg.getBytes(/*YOUR_CHARSET?*/));}
@Richard我认为您最好使用类com.notnoop.apns.internal.Utilities
service.push(Utilities.encodeHex(topics.getBytes()),payload)中的方法代码>
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: {}
};
push(Collection<String> deviceTokens, String payload)
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 }