表情符号在iOS推送通知中无法正确显示。

huangapple 未分类评论70阅读模式
英文:

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 = &quot;\uD83D\uDE0A&quot;;

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 &quot;\xE2\x9A\xA1&quot; 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 = &quot;/send&quot;, method = RequestMethod.GET, produces = &quot;application/json&quot;)
    public static ResponseEntity&lt;String&gt; send(String message, String deviceToken, Integer type, Object response, String userNameAsTitle) {

        //response.setMessage(message);
        //response.setNotificationType(type);

        JSONObject body = new JSONObject();
        body.put(&quot;to&quot;, deviceToken);
        body.put(&quot;priority&quot;, &quot;high&quot;);

        Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
        map.put(&quot;title&quot;, userNameAsTitle);
        map.put(&quot;body&quot;, message);
        //map.put(&quot;alert&quot;, descprtion);
        map.put(&quot;type&quot;, type);
        map.put(&quot;badge&quot;, 1);

        map.put(&quot;sound&quot;, &quot;default&quot;);
        map.put(&quot;response&quot;, response);

        JSONObject notification = new JSONObject(map);
        //body.put(&quot;notification&quot;, notification);
        body.put(&quot;notification&quot;, notification);

        HttpEntity&lt;String&gt; request = new HttpEntity&lt;&gt;(body.toString());

        CompletableFuture&lt;String&gt; pushNotification = androidPushNotificationsService.send(request);
        CompletableFuture.allOf(pushNotification).join();
        try {
            String firebaseResponse = pushNotification.get();
            System.out.println(firebaseResponse.toString());
            return new ResponseEntity&lt;&gt;(firebaseResponse.toString(), HttpStatus.OK);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        return new ResponseEntity&lt;&gt;(&quot;Push Notification ERROR!&quot;, HttpStatus.BAD_REQUEST);
    }

huangapple
  • 本文由 发表于 2020年8月14日 20:15:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63412590.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定