djacobs / PyAPNs

Python library for interacting with the Apple Push Notification service (APNs)
http://pypi.python.org/pypi/apns/
MIT License
1.22k stars 374 forks source link

How do you send an emoji using Python pyapns #127

Open markmark1 opened 9 years ago

markmark1 commented 9 years ago

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

jimhorng commented 9 years ago

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

markmark1 commented 9 years ago

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 commented 9 years ago

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 notifications@github.com 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 commented 8 years ago

Is it possible to give an example please

simonw commented 8 years ago

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')
ExplodingCabbage commented 8 years ago

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?

djacobs commented 8 years ago

@markmark1 @ExplodingCabbage is this still open for you ?

ExplodingCabbage commented 8 years ago

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.