如果字符限制超过 80 个字符,则不会发送推送通知

Push notification not delivered if character limit exceeds 80 characters

为了发送推送通知,我们使用了Amazon Simple Notification Service (Amazon SNS)。当我通过发送大约 80 个字符来测试推送通知时,我收到推送通知,但当字符超过 80 或 85 个时,通知不会被发送。

我们对有效载荷有 256 字节的限制,但我认为如果我发送那么多字符,它可能不会超过该限制。至少应该截断消息。

我发现:

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.

但就我而言,我什至没有收到通知。它与 Amazon SNS 相关吗?我是否遗漏了要检查的内容?

编辑 1:

我不会在短信中附加图片或任何内容。我只是发送纯文本消息。

编辑 2:

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 9。因此,对于该设备,2000 字节 远远超过 80-85 个字符(包括负载大小)的限制。

我真的很绝望我错过了什么?

您应该记住,256 字节的限制是针对 整个 有效载荷,因此不仅是您的消息,还有其他所有内容 - 有效载荷采用 JSON 格式, so keys,所有特殊字符也算到极限。

这是 Apple 认为正确的最低负载要求:

{
    "aps" : {
        "alert" : "your text"
    }
}

所以我们已经 "loose" 19 个字节,发送一个简单的通知。如果我们还想有一个自定义标题:

{
    "aps" : {
        "alert" : {
            "title" : "your title",
            "body" : "your text"
        }
    }
}

这总共有 40 "lost" 个字节(大约 15%)。添加自定义声音和徽章也会减少实际消息的剩余计数。

现在,这些字节仅由于所需的密钥而丢失,对此您无能为力。我没有使用过 Amazon SNS,但他们可能会为自己的目的添加一些自定义字段,从而使您的消息少 space。您可以通过检查 userInfo 字典在 didReceiveRemoteNotification 方法中检查它。简单 NSLog(@"userInfo -> %@", userInfo) 应该将所有内容转储到控制台。这种表示在额外字符方面不会 1:1 与 JSON,但会让您了解除了发送必填字段之外还有什么(如果有的话)。

另一件值得一提的事情是,non-ASCII 个字符将占用 space 个字节以上的空间,因此您可以有效地在消息中使用更少的字符。