What is the maximum length of a Push Notification alert text?

后端 未结 11 1197
北海茫月
北海茫月 2020-11-30 16:28

What is the maximum length of the alert text of an iOS push notification?

The documentation states that the notification payload has to be under 256 bytes in total,

相关标签:
11条回答
  • 2020-11-30 17:04

    The real limits for the alert text are not documented anywhere. The only thing the documentation says is:

    In iOS 8 and later, the maximum size allowed for a notification payload is 2 kilobytes; Apple Push Notification Service refuses any notification that exceeds this limit. (Prior to iOS 8 and in OS X, the maximum payload size is 256 bytes.)

    This is what I could find doing some experiments.

    • Alerts: Prior to iOS 7, the alerts display limit was 107 characters. Bigger messages were truncated and you would get a "..." at the end of the displayed message. With iOS 7 the limit seems to be increased to 235 characters. If you go over 8 lines your message will also get truncated.
    • Banners: Banners get truncated around 62 characters or 2 lines.
    • Notification Center: The messages in the notification center get truncated around 110 characters or 4 lines.
    • Lock Screen: Same as a notification center.

    Just as a reminder here is a very good note from the official documentation:

    If necessary, iOS truncates your message so that it fits well in each notification delivery style; for best results, you shouldn’t truncate your message.

    0 讨论(0)
  • 2020-11-30 17:07

    Apple push will reject a string for a variety of reasons. I tested a variety of scenarios for push delivery, and this was my working fix (in python):

    #  Apple rejects push payloads > 256 bytes (truncate msg to < 120 bytes to be safe)
    if len(push_str) > 120:
        push_str = push_str[0:120-3] + '...'
    
    # Apple push rejects all quotes, remove them
    import re
    push_str = re.sub("[\"']", '', push_str)
    
    # Apple push needs to newlines escaped
    import MySQLdb
    push_str = MySQLdb.escape_string(push_str)
    
    # send it
    import APNSWrapper
    wrapper = APNSWrapper.APNSNotificationWrapper(certificate=...)
    message = APNSWrapper.APNSNotification()
    message.token(...)
    message.badge(1)
    message.alert(push_str)
    message.sound("default")
    wrapper.append(message)
    wrapper.notify()
    
    0 讨论(0)
  • 2020-11-30 17:09

    For regular remote notifications, the maximum size is 4KB (4096 bytes) https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

    ###iOS the size limit is 256 bytes, but since the introduction of iOS 8 has changed to 2kb!

    https://forums.aws.amazon.com/ann.jspa?annID=2626

    With iOS 8, Apple introduced new features that enable some rich new use cases for mobile push notifications — interactive push notifications, third party widgets, and larger (2 KB) payloads. Today, we are pleased to announce support for the new mobile push capabilities announced with iOS 8. We are publishing a new iOS 8 Sample App that demonstrates how these new features can be implemented with SNS, and have also implemented support for larger 2KB payloads.

    0 讨论(0)
  • 2020-11-30 17:12

    According to the WWDC 713_hd_whats_new_in_ios_notifications. The previous size limit of 256 bytes for a push payload has now been increased to 2 kilobytes for iOS 8.

    Source: http://asciiwwdc.com/2014/sessions/713?q=notification#1414.0

    0 讨论(0)
  • 2020-11-30 17:21

    It should be 236 bytes. There is no restriction on the size of the alert text as far as I know, but only the total payload size. So considering if the payload is minimal and only contains the alert information, it should look like:

    {"aps":{"alert":""}}
    

    That takes up 20 characters (20 bytes), leaving 236 bytes to put inside the alert string. With ASCII that will be 236 characters, and could be lesser with UTF8 and UTF16.

    0 讨论(0)
  • 2020-11-30 17:21

    Here're some screenshots (banner, alert, & notification center)

    AlertBannerNotification Center

    0 讨论(0)
提交回复
热议问题