Burak
Burak

Reputation: 5764

How to send IOS Push Notifications with Emoji in message in a NodeJS server?

I write some server code to send push notifications. I have a form field, called message. What I need is, putting some Emoji characters in the message.

If I write \ue48d , for a cloud character, server see it as a string '\ue48d'.

How can I decode it so I can use it in a push notification?

Update: Here is my server code. I want to write \exxx to the form's message field, and encode it in this code then send to Apple.

exports.create = function(req, res){
    var devices = req.body.devices;
    var message = req.body.message;
    var note = new apn.Notification();
    note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
    note.badge = 3;
    note.sound = "ping.aiff";
    note.alert =  message;
    note.payload = {'messageFrom': 'Burak'}; 
    for (var i in devices) {
      device =  new apn.Device(devices[i]);
      apnConnection.pushNotification(note, device);
    }

    res.send(200,'Successfull')
}

Upvotes: 0

Views: 4214

Answers (1)

Danack
Danack

Reputation: 25711

You keep saying different things. You first said:

If I write \ue48d , for a cloud character, server see it as a string '\ue48d'.

Now you're saying

I write "\e415 Hello!" to the message field.

At the risk of pointing out the obvious \ue48d != \e415

UTF8 characters in JSON are encoded like with \u and then the character code i.e. \ue48d is a valid character to send to the phone in JSON. \e415 is not, which is why your phone is not displaying it as an emoji character.

So, what does your phone see when you send '\ue48d' to it? i.e. what does it see when you send it the original correctly encoded string.

Edit

If it works when you send a properly encoded string from the server, then you probably just need to decode the message sent to the server, before sending it back out i.e.

message = JSON.parse(message);

If the message your server is receiving is just a string, then that should just work as is. If the message is an array or an object, then you will need to pick out the appropriate entry to send to the phone as the message.

Upvotes: 1

Related Questions