iOS 7 中的静默推送通知不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19239737/
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
Silent Push Notification in iOS 7 does not work
提问by mkwon
In the WWDC 2013's "What's New with Multitasking" presentation, there is a section about Silent Push Notifications. It seems straight forward. According to the presentation, if you send the APS payload with just the content-available set to 1, users will not be notified of the notification.
在 WWDC 2013 的“多任务新功能”演示中,有一节是关于静默推送通知的。看起来很直接。根据介绍,如果您发送 APS 负载时仅将 content-available 设置为 1,则用户将不会收到通知。
// A. This doesn't work
{
aps: {
content-available: 1
}
}
My testing shows that this does not work as no push is received. But if I include the sound attribute but exclude the alert attribute, it works (though not silent anymore).
我的测试表明这不起作用,因为没有收到推送。但是如果我包含 sound 属性但排除 alert 属性,它就可以工作(虽然不再沉默)。
// B. This works
{
aps: {
content-available: 1,
sound: "default"
}
}
However, if I change the sound attribute to play a silent audio, I can mimic a silent push.
但是,如果我更改 sound 属性以播放无声音频,则可以模拟无声推送。
// C. This works too.
{
aps: {
content-available: 1,
sound: "silence.wav"
}
}
Does anyone know:
有人知道吗:
- If this a bug?
- And if it is correct to assume that B or C is being treated as a Remote Notification (and not a bug with Silent Push where you need a sound attribute)? If so, this means it is not rate limited like Silent Pushes are... which Apple will likely fix. So I probably should not rely on it.
- What the rate limit is (N pushes every X seconds, etc)?
- 如果这是一个错误?
- 如果假设 B 或 C 被视为远程通知是正确的(而不是需要声音属性的静默推送的错误)?如果是这样,这意味着它不会像 Silent Pushes 那样受到速率限制...... Apple 可能会解决这个问题。所以我可能不应该依赖它。
- 速率限制是多少(N 每 X 秒推送一次,等等)?
Thanks in advance.
提前致谢。
Edit with more information
使用更多信息进行编辑
For A, the state of the application does not matter. Notification is never received.
对于 A,应用程序的状态无关紧要。永远不会收到通知。
It seems like B and C only work if you enclose the attributes and values in quotes, like below.
似乎 B 和 C 只有在将属性和值括在引号中时才有效,如下所示。
{"aps":{"content-available": 1, "sound":"silent.wav"}}
And the notification arrives in application:didReceiveRemoteNotification:fetchCompletionHandler:regardless of state.
并且通知到达application:didReceiveRemoteNotification:fetchCompletionHandler:无论状态如何。
采纳答案by CMVR
This works also and does not play a sound when it arrives:
这也有效,并且在到达时不播放声音:
{
aps = {
"content-available" : 1,
sound : ""
};
}
EDIT
编辑
People having this problem may want to check out this link. I have been participating in a thread on Apple's Developer forum that goes over all app states and when silent pushes are received and not received.
遇到此问题的人可能想查看此链接。我一直在参与 Apple 开发者论坛上的一个帖子,该帖子讨论了所有应用程序状态以及何时收到和未收到静默推送。
回答by Dave Lyon
So I just came across this issue yesterday, and after trying sending a payload with a sound set to an empty string, it was still causing vibration/sound on the device. Eventually, I stumbled on a blog post from Urban Airship that suggested needing to send:
所以我昨天刚遇到这个问题,在尝试发送一个声音设置为空字符串的有效载荷后,它仍然在设备上引起振动/声音。最终,我偶然发现了 Urban Airship 的一篇博客文章,其中建议需要发送:
{ priority: 5 }
in the push notification, which I had never seen. After perusing Apple's docs for push notifications, I stumbled on this page:
在我从未见过的推送通知中。在仔细阅读 Apple 的推送通知文档后,我偶然发现了这个页面:
Which indicates that priority should be set as "5" or "10", and explains:
表示优先级应设置为“5”或“10”,并说明:
The notification's priority. Provide one of the following values:
10 The push message is sent immediately.
The push notification must trigger an alert, sound, or badge on the device. It is an error to use this priority for a push that contains only the content-available key.
5 The push message is sent at a time that conserves power on the device receiving it.
通知的优先级。提供以下值之一:
10 推送消息立即发送。
推送通知必须在设备上触发警报、声音或徽章。将此优先级用于仅包含内容可用密钥的推送是错误的。
5 推送消息的发送时间可以节省接收它的设备的电量。
Ultimately, we were able to get silent push notifications working with a badge count (and I suspect you could even do the same with an alert) with the following format:
最终,我们能够使用以下格式获得带有徽章计数的静默推送通知(我怀疑你甚至可以用警报做同样的事情):
aps = {
badge = 7;
"content-available" = 1;
priority = 5;
};
回答by JaimeFBC
I have tried setting an empty string as the alert attribute and it also worked:
我尝试将空字符串设置为警报属性,它也有效:
{
aps = {
"content-available" = 1;
"alert" = "";
};
}
It seems like APNS is checking for the existence of this attributes for the purpose of validating the push payload. Interestingly, they are not checking the actual content. It seems a little bit hacky though...
似乎 APNS 正在检查此属性是否存在以验证推送有效负载。有趣的是,他们并没有检查实际内容。虽然看起来有点hacky...
回答by wenghengcong
I use the tool-Knuffsend my push notification to my device.
我使用工具- Knuff将我的推送通知发送到我的设备。
Then,I tried these example.
然后,我尝试了这些示例。
They are all work!But you must set the priority 10!
他们都是工作!但你必须设置优先级10!
So if you are not use the tool,you also note it.
所以如果你不使用这个工具,你也要注意它。
examples:
例子:
- no alert,no sound
- 没有警报,没有声音
{
"aps":{
"content-available":1,
}
}
- only alert
- 只提醒
{
"aps":{
"content-available":1,
"alert":""
}
}
- only sound
- 只有声音
{
"aps":{
"content-available":1,
"sound":""
}
}
回答by IgniteCoders
This works for me:
这对我有用:
{
aps: {
content-available: 1
}
}
Look if you check Background fetch
checkbox in Project Capabilities
> Background Modes
看看您是否Background fetch
选中了Project Capabilities
> 中的复选框Background Modes
回答by Jon C
I'm seeing the same problem. If I send a push with "content-available":1 and no other attributes set, the notification is never received. When I add any other attributes it works perfectly.
我看到了同样的问题。如果我发送带有“content-available”:1 且未设置其他属性的推送,则永远不会收到通知。当我添加任何其他属性时,它可以完美运行。
As a temporary work around I'm adding the badge attribute as this doesn't alert the user in any way apart from adding the badge to the icon.
作为临时解决方法,我添加了徽章属性,因为除了将徽章添加到图标之外,这不会以任何方式提醒用户。
Let me know if you've found a better solution.
如果您找到了更好的解决方案,请告诉我。
回答by dev03
Priority should be set as one item in binary stream but not in payload json string. Apparently only the latest type 2 format can be used in setting priority as follows:
优先级应设置为二进制流中的一项,而不是有效负载 json 字符串中的一项。显然只有最新的类型 2 格式可以用于设置优先级,如下所示:
$token = chr(1) . pack('n', 32) . pack('H*', $deviceToken);
$payload = chr(2) . pack('n', strlen($json)) . $json;
$identifier = chr(3) . pack('n', 4) . pack('N', $notification);
$expiration = chr(4) . pack('n', 4) . pack('N', time()+86400);
$priority = chr(5) . pack('n', 1) . chr($priority);
$frame_data = $token.$payload.$identifier.$expiration.$priority;
$frame_length = strlen(bin2hex($frame_data))/2;
$msg = chr(2) . pack('N', $frame_length) . $frame_data;
Format types (first byte) for remote notification binary message:
远程通知二进制消息的格式类型(第一个字节):
0 - simple (old) 1 - enhanced (old) 2 - latest with more parameters (new)
0 - 简单(旧) 1 - 增强(旧) 2 - 具有更多参数的最新(新)
回答by Radim
setting priority to 5 did not work for me, but setting sound or alert to an empty string did cause the notification to be handled as a high priority one
将优先级设置为 5 对我不起作用,但是将声音或警报设置为空字符串确实会导致通知被作为高优先级处理
回答by software evolved
Argh! Also pulling my hair out -- this isn't so much an answer as another example of a payload which DOESN'T work. The didReceiveRemoteNotification method is never called, although if the device is sleeping, the alert text IS displayed.
啊!也拉我的头发 - 这与其说是一个答案,不如说是另一个不起作用的有效载荷示例。didReceiveRemoteNotification 方法永远不会被调用,但如果设备处于睡眠状态,则会显示警报文本。
{"aps":
{ "alert":"alert!",
"sound":"default",
"content-available" : 1},
"content-id":21482,
"apt":"1"
}
"apt" is a custom field we use to indicate the notification type.
“apt”是我们用来指示通知类型的自定义字段。
回答by Alex Zak
Setting 'sound' to 0 worked for me... :)
将“声音”设置为 0 对我有用... :)