ios 推送通知警报文本的最大长度是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6307748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:16:57  来源:igfitidea点击:

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

iphoneiospush-notification

提问by hpique

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

iOS 推送通知的警报文本的最大长度是多少?

The documentationstates that the notification payload has to be under 256 bytes in total, but surely there must be a specific character limit for the alert text.

文档指出,通知有效负载总共必须少于 256 个字节,但警报文本肯定必须有特定的字符限制。

回答by ascandroli

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

警报文本的实际限制未在任何地方记录。文档中唯一说的是:

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.)

在 iOS 8 及更高版本中,通知负载允许的最大大小为 2 KB;Apple 推送通知服务拒绝任何超过此限制的通知。(在 iOS 8 和 OS X 之前,最大有效负载大小为 256 字节。)

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.
  • 警报:在 iOS 7 之前,警报显示限制为 107 个字符。较大的消息被截断,您会在显示的消息末尾得到一个“...”。在 iOS 7 中,限制似乎增加到 235 个字符。如果超过 8 行,您的消息也会被截断。
  • 横幅:横幅会被截断大约 62 个字符或 2 行。
  • 通知中心:通知中心的消息被截断约 110 个字符或 4 行。
  • 锁屏:与通知中心相同。

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.

如有必要,iOS 会截断您的消息,使其适合每种通知传递方式;为获得最佳结果,您不应截断您的消息。

回答by Anurag

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:

它应该是 236 字节。据我所知,警报文本的大小没有限制,但只有总负载大小。因此,考虑到有效负载是否最小且仅包含警报信息,它应该如下所示:

{"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.

这占用了 20 个字符(20 个字节),剩下 236 个字节放在警报字符串中。ASCII 为 236 个字符,UTF8 和 UTF16 可能更少。

回答by William Denniss

The limit of the enhanced format notifications is documented here.

此处记录了增强格式通知的限制。

It explicitly states:

它明确指出:

The payload must not exceed 256 bytes and must not be null-terminated.

有效载荷不得超过 256 字节,并且不得以空字符结尾。

ascandroli claims abovethat they were able to send messages with 1400 characters. My own testing with the new notification format showed that a message just 1 byte over the 256 byte limit was rejected. Given that the docs are very expliciton this point I suggest it is safer to use 256 regardless of what you may be able to achieve experimentally as there is no guarantee Apple won't change it to 256 in the future.

ascandroli上面声称他们能够发送 1400 个字符的消息。我自己对新通知格式的测试表明,超过 256 字节限制仅 1 个字节的消息被拒绝。鉴于文档在这一点上非常明确,我建议使用 256 更安全,无论您可以通过实验实现什么,因为无法保证 Apple 将来不会将其更改为 256。

As for the alert text itself, if you can fit it in the 256 total payload size then it will be displayed by iOS. They truncate the message that shows up on the status bar, but if you open the notification center, the entire message is there. It even renders newline characters \n.

至于警报文本本身,如果您可以将其容纳在 256 个总负载大小中,那么它将由 iOS 显示。它们会截断状态栏上显示的消息,但如果您打开通知中心,则整个消息都在那里。它甚至呈现换行符\n

回答by jcesarmobile

EDIT:

编辑:

Updating the answer with latest information

最新信息更新答案

The maximum size allowed for a notification payload depends on which provider API you employ.

通知负载允许的最大大小取决于您使用的提供程序 API。

When using the legacy binary interface, maximum payload size is 2KB (2048 bytes).

使用旧版二进制接口时,最大负载大小为 2KB(2048 字节)。

When using the HTTP/2 provider API, maximum payload size is 4KB (4096 bytes). For Voice over Internet Protocol (VoIP) notifications, the maximum size is 5KB (5120 bytes)

使用 HTTP/2 提供程序 API 时,最大负载大小为 4KB(4096 字节)。对于互联网协议语音 (VoIP) 通知,最大大小为 5KB(5120 字节)

OLD ANSWER: According to the apple doc the payload for iOS 8 is 2 kilobytes (2048 bytes) and 256 bytes for iOS 7 and prior. (removed the link as it was an old doc and it's broken now)

旧答案:根据苹果文档,iOS 8 的有效载荷为 2 KB(2048 字节),iOS 7 及更早版本的有效载荷为 256 字节。(删除了链接,因为它是旧文档,现在已损坏)

So if you just send text you have 2028 (iOS 8+) or 236 (iOS 7-) characters available.

因此,如果您只发送文本,则有 2028 个(iOS 8+)或 236 个(iOS 7-)字符可用。

The Notification Payload

Each remote notification includes a payload. The payload contains information about how the system should alert the user as well as any custom data you provide. 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.)

通知负载

每个远程通知都包含一个有效负载。有效负载包含有关系统应如何提醒用户以及您提供的任何自定义数据的信息。在 iOS 8 及更高版本中,通知负载允许的最大大小为 2 KB;Apple 推送通知服务拒绝任何超过此限制的通知。(在 iOS 8 和 OS X 之前,最大有效负载大小为 256 字节。)

But I've tested and you can send 2 kilobytes to iOS 7 devices too, even in production configurations

但我已经测试过,即使在生产配置中,您也可以向 iOS 7 设备发送 2 KB

回答by Loozie

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

这是一些屏幕截图(横幅、警报和通知中心)

AlertBannerNotification Center

警报横幅通知中心

回答by Jorgesys

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

对于常规远程通知,最大大小为4KB(4096 字节)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!

###iOS 的大小限制是 256 字节,但自从 iOS 8 的引入已更改为 2kb!

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

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.

在 iOS 8 中,Apple 引入了一些新功能,为移动推送通知启用了一些丰富的新用例 - 交互式推送通知、第三方小部件和更大 (2 KB) 的有效负载。今天,我们很高兴地宣布支持 iOS 8 发布的新移动推送功能。我们将发布一个新的 iOS 8 示例应用程序,演示如何通过 SNS 实现这些新功能,并且还实现了对更大 2KB 负载的支持。

回答by klefevre

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

根据 WWDC 713_hd_whats_new_in_ios_notifications。之前推送负载的256 字节大小限制现在已增加到iOS 8 的2 KB

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

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

回答by Philip Fung

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):

苹果推送会因为各种原因拒绝一个字符串。我测试了各种推送交付场景,这是我的工作修复(在 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()

回答by Burak

According to updated Apple document(check my answer date):

根据更新的 Apple 文档(检查我的回答日期):

"... When using the HTTP/2 provider API, maximum payload size is 4096 bytes. Using the legacy binary interface, maximum payload size is 2048 bytes. Apple Push Notification service (APNs) refuses any notification that exceeds the maximum size."

“...当使用 HTTP/2 提供程序 API 时,最大有效载荷大小为 4096 字节。使用传统二进制接口,最大有效载荷大小为 2048 字节。Apple 推送通知服务 (APNs) 拒绝任何超过最大大小的通知。”

回答by SaRaVaNaN DM

Apple Updated Doc:

苹果更新文档:

Each remote notification includes a payload. The payload contains information about how the system should alert the user as well as any custom data you provide. The maximum size allowed for a notification payload depends on which provider API you employ. When using the HTTP/2 provider API, maximum payload size is 4096 bytes. Using the legacy binary interface, maximum payload size is 2048bytes. Apple Push Notification service (APNs) refuses any notification that exceeds the maximum size.

每个远程通知都包含一个有效负载。有效负载包含有关系统应如何提醒用户以及您提供的任何自定义数据的信息。通知负载允许的最大大小取决于您使用的提供程序 API。使用 HTTP/2 提供程序 API 时,最大有效负载大小为 4096 字节。使用旧版二进制接口,最大负载大小为 2048字节。Apple 推送通知服务 (APNs) 拒绝任何超过最大大小的通知。