2017-06-22 5 views
0

Ich verwende Java 8 mit Spring und Maven.Java Ungültiges Hexadezimalzeichen: h

Ich versuche, Push-Benachrichtigungen basierend auf der following tutorial einzurichten. Ich habe sie gut funktionierend für Android, mit dem folgenden:

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; 
} 

Ich bin jedoch ein Problem mit versuchen, es einzurichten für iOS. Ich erhalte die folgende Fehlermeldung:

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) 

Mein Code für iOS sich wie folgt:

/** 
* 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; 
} 

Wie Sie sehen können, tritt der Fehler als Folge der Codezeile auf der Leitung 90.

service.push(topics, payload); 

ich denke, das ein Ergebnis des Codes ist verschieden von den tutorial, der wie folgt lautet:

service.push(DEVICE_TOKEN, payload); 

Ich brauche meinen Benachrichtigungsdienst eher ein topic zu verwenden und nicht ein device token (der Kunde abonniert ein topic). Dies funktioniert erfolgreich für Android wie Sie oben sehen können.

Frage

Wenn jemand etwas Licht auf vergießen kann, wie ich dieses iOS Problem beheben kann, würde ich die Hilfe zu schätzen wissen.

Danke

UPDATE

ich ein wenig mehr Info hinzufügen wird, weil aus den Kommentaren unten (Dank für die Hilfe), wie mir gesagt, dass der Parameter ein devise token sein sollte und nicht ein topic.

Mein Problem ist mein Client abonniert ein Thema. Das ist Geschäftslogik, die ich nicht ändern kann.

 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: {} 
     }; 

Ich sehe die com.notnoop.apns.ApnsService ein Collection<String> von devise Token erhält.

push(Collection<String> deviceTokens, String payload) 

Ich werde versuchen, legte meine topicString in einem Collection, und sehen, ob das funktioniert.

+0

Warum die down vote?Ist das keine berechtigte Frage? – Richard

+0

Haben Sie das Debuggen versucht? Was wird in der Variable 'Nutzlast' gespeichert? – Lemonov

+0

* Ungültiges Hex-Zeichen: h * Ich würde sagen, Sie müssen einen Hex-Wert senden, aber Sie senden eine Zeichenfolge – Jens

Antwort

2

Wie Sie im src code sehen:

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  } 

die devicetoken muss hex codiert werden.

+0

Danke, das hat das Problem für mich gelöst. Ich habe das Thema in Hex umgewandelt. 'public String toHex (String arg) { Rückgabe String.format ("% 040x ", neuer BigInteger (1, arg.getBytes (/ * YOUR_CHARSET? * /))); } ' – Richard

+0

@Richard Ich denke, Sie sollten besser verwenden Sie die Methode' encodeHex' aus der Klasse 'com.notnoop.apns.internal.Utilities' – Jens

+0

Danke, wird – Richard

Verwandte Themen