PyAPNs icon indicating copy to clipboard operation
PyAPNs copied to clipboard

How do you send an emoji using Python pyapns

Open markmark1 opened this issue 10 years ago • 8 comments

Passing Unicode UTF8 string to alert or body or title parameter doesn't work if an example on how to construct payload in Python is giving will be great

markmark1 avatar Jun 25 '15 06:06 markmark1

per #29 , you can try to encode your utf-8 string first before passing to APNS, FYR

jimhorng avatar Jun 26 '15 15:06 jimhorng

Can you please give an example of sending emoji if I use Unicode('😀😀','utf8') just gives a string of gibberish text in push not the emoji

markmark1 avatar Jun 26 '15 15:06 markmark1

Can you pls give an example on how you would encode alert or body or title or do we use loc-args

On Fri, Jun 26, 2015 at 8:13 AM, Jim Horng [email protected] wrote:

per #29 , you can try to encode your utf-8 string first before passing to APNS, FYR

Reply to this email directly or view it on GitHub: https://github.com/djacobs/PyAPNs/issues/127#issuecomment-115723462

markmark1 avatar Jun 26 '15 15:06 markmark1

Is it possible to give an example please

markmark1 avatar Mar 15 '16 03:03 markmark1

I tracked this issue down to the .json() method in the Payload class. Here's a subclass which I've just tested and allows Emoji to be sent:

class EmojiEnabledPayload(Payload):
    def json(self):
        # The default Payload.json implementation sets ensure_ascii=False
        # which results in emoji failing to send.
        return json.dumps(self.dict(), separators=(',', ':'), ensure_ascii=True).encode('utf-8')

simonw avatar May 13 '16 10:05 simonw

Unfortunately https://github.com/djacobs/PyAPNs/issues/167 has the intolerable effect of ratcheting up the payload size of non-ASCII messages considerably, so I've rejected it in its current form. However, I'd still like to see this solved.

I admit I am very confused, and unfortunately I have no access to an iOS device to test on currently.

I don't understand why this bug exists. Why should emojis need different handling to other non-ASCII characters?

Is Apple treating JSON escape sequences in JSON strings differently from UTF-8 representations of the exact same characters inside those JSON strings? That would be truly bizarre, and nobody has outright said it yet, but if @simonw's solution works then surely that must be what is going on?

I gather that there is some complicated history to unicode emojis; for instance, if I look up the Pile of Poo on iemoji.com it has a history section that notes that the Pile of Poo's unicode code point has changed over time:

  1. This emoji was part of the proprietary / non-standardized emoji set first introduced by Japanese carriers like Softbank. These emojis became part of the Apple iPhone starting in iOS 2.2 as an unlockable feature on handsets sold in English speaking countries.
  2. In iOS 5 / OSX 10.7, the underlying code that the Apple OS generates for this emoji was changed.

Is this history relevant in any way? I don't see any obvious link, but it's odd that this bug apparently only affects emojis?

@simonw, do you understand what's going on any better than I do? If so, would you be willing to provide an overview, and any thoughts you have on how best to approach solving the bug?

ExplodingCabbage avatar May 14 '16 11:05 ExplodingCabbage

@markmark1 @ExplodingCabbage is this still open for you ?

djacobs avatar Aug 12 '16 13:08 djacobs

https://github.com/djacobs/PyAPNs/pull/167 was the only attempt that has been made to fix it, and I rejected it (as it introduced worse problems), so I assume that this is still an issue.

ExplodingCabbage avatar Aug 12 '16 13:08 ExplodingCabbage