英文:
Emojis not rendering properly in iOS push notifications
问题
I have a Java Spring Boot web service and I am trying to send push notifications to an iOS device.
The problem I am facing is that the emoji text, which is directly pasted, like:
String emoji = "😀";
Or its code, like:
String emoji = "\uD83D\uDE0A";
It shows as ?
(Question mark symbol).
I have tried getting it as bytes of UTF_8 characters like this:
byte[] emojis = user.getEmoji().getBytes();
String emojisAsString = new String(emojis, StandardCharsets.UTF_8);
Integer emojiCodePoint = emojisAsString.codePointAt(emojisAsString.offsetByCodePoints(0,0));
char emojiChars[] = {Character.highSurrogate(emojiCodePoint), Character.lowSurrogate(emojiCodePoint)};
But it still shows as question marks.
I also tried using UTF-8 code like:
"\xE2\x9A\xA1"
But this one just got printed as it is on the notification.
Also, when I call the notification API from Postman using FCM APIs and paste emojis, it shows emojis in the notification. It just shows as question marks when done through the Java web service.
This is the Push notification service code:
@RequestMapping(value = "/send", method = RequestMethod.GET, produces = "application/json")
public static ResponseEntity<String> send(String message, String deviceToken, Integer type, Object response, String userNameAsTitle) {
JSONObject body = new JSONObject();
body.put("to", deviceToken);
body.put("priority", "high");
Map<String, Object> map = new HashMap<>();
map.put("title", userNameAsTitle);
map.put("body", message);
map.put("type", type);
map.put("badge", 1);
map.put("sound", "default");
map.put("response", response);
JSONObject notification = new JSONObject(map);
body.put("notification", notification);
HttpEntity<String> request = new HttpEntity<>(body.toString());
CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
CompletableFuture.allOf(pushNotification).join();
try {
String firebaseResponse = pushNotification.get();
System.out.println(firebaseResponse.toString());
return new ResponseEntity<>(firebaseResponse.toString(), HttpStatus.OK);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
英文:
I have a Java Spring Boot web service and I am trying to send push notifications to an iOS device.
The problem I am facing is that the emoji text Which is directly pasted, like
<!-- language-all: java -->
String emoji = "😀";
Or its code, like
String emoji = "\uD83D\uDE0A";
it shows as ?
(Question mark symbol)
I have tried getting it as bytes of UTF_8 characters like this:
byte[] emojis = user.getEmoji().getBytes(); //Here user.getEmoji() returns a collection of 2-3 emojis
String emojisAsString = new String(emojis, StandardCharsets.UTF_8);
Integer emojiCodePoint = emojisAsString.codePointAt(emojisAsString.offsetByCodePoints(0,0));
char emojiChars[] = {Character.highSurrogate(emojiCodePoint), Character.lowSurrogate(emojiCodePoint)};
But it still shows as question marks.
I also tried using UTF-8 code like "\xE2\x9A\xA1"
but this one just got printed as it is on the notification.
Also when I call the notification API from postman using FCM APIs and paste emojis, it shows emojis in the notification, it just shows as question marks when done through Java web service.
This is the Push notification service code
@RequestMapping(value = "/send", method = RequestMethod.GET, produces = "application/json")
public static ResponseEntity<String> send(String message, String deviceToken, Integer type, Object response, String userNameAsTitle) {
//response.setMessage(message);
//response.setNotificationType(type);
JSONObject body = new JSONObject();
body.put("to", deviceToken);
body.put("priority", "high");
Map<String, Object> map = new HashMap<>();
map.put("title", userNameAsTitle);
map.put("body", message);
//map.put("alert", descprtion);
map.put("type", type);
map.put("badge", 1);
map.put("sound", "default");
map.put("response", response);
JSONObject notification = new JSONObject(map);
//body.put("notification", notification);
body.put("notification", notification);
HttpEntity<String> request = new HttpEntity<>(body.toString());
CompletableFuture<String> pushNotification = androidPushNotificationsService.send(request);
CompletableFuture.allOf(pushNotification).join();
try {
String firebaseResponse = pushNotification.get();
System.out.println(firebaseResponse.toString());
return new ResponseEntity<>(firebaseResponse.toString(), HttpStatus.OK);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
return new ResponseEntity<>("Push Notification ERROR!", HttpStatus.BAD_REQUEST);
}
专注分享java语言的经验与见解,让所有开发者获益!
评论