Emojis not rendering properly in iOS push notifications
Asked Answered
C

0

7

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(); //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);
    }
Colorcast answered 14/8, 2020 at 12:15 Comment(3)
Could you add the code for the service as well?Hawse
Added the send method for the push notification. Here in the last parameter I want to pass emojis. i.e. userNameAsTitleColorcast
Is this a duplicate of https://mcmap.net/q/1629328/-emoji-does-not-display-in-push-notification-instead-it-display-question-mark-or-display-unicode/535401, which was asked on the same day by a user with the same last name and which also has a +50 bounty? Neither question has an answer, so I'm not allowed to flag it as a duplicate for moderator evaluation, but it seems clear that we don't need both.Laminar

© 2022 - 2024 — McMap. All rights reserved.