iOS 中的远程通知和静默通知有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42275060/
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
What is difference between remote notification and silent notification in iOS?
提问by transang
When I read Apple Docs, they mention 3 types of notification: local, remote, and silent.
当我阅读Apple Docs 时,他们提到了 3 种类型的通知:本地、远程和静默。
Local notification can be inferred from its name, that is sent by the app locally.
本地通知可以从它的名字推断出来,它是由应用程序在本地发送的。
However, what is the difference of the other two types?
但是,其他两种类型有什么区别呢?
回答by Honey
EDIT:While this answer is fully applicable, there are some additions(not changes) to notifications in iOS 12. I highly recommend watching WWDC 2018: What's New in User Notificationsand read this amazing and must read article.
编辑:虽然此答案完全适用,但iOS 12 中的通知有一些添加(而非更改)。我强烈建议您观看WWDC 2018:用户通知中的新功能并阅读这篇精彩且必须阅读的文章。
Main changes are:
主要变化有:
- grouped notifications along with summary format
- provisional notifications ie show notifications directly in notification center without user permission
- critical notifications which ignores 'do not disturb' or 'mute'
- ability to interact with the notifications in the extensions
- ability to completely reset or update actions
- ability to deeplink into app's notification Settings from phone's Notification Center
- 分组通知以及摘要格式
- 临时通知,即未经用户许可直接在通知中心显示通知
- 忽略“请勿打扰”或“静音”的重要通知
- 与扩展中的通知交互的能力
- 完全重置或更新操作的能力
- 能够从手机的通知中心深层链接到应用程序的通知设置
IMPORTANT NOTE:Not sure since when but from the Apple docs, the 'silent notification' has been renamedto 'background notification'
重要说明:不确定从什么时候开始,但从 Apple 文档中,“静默通知”已重命名为“后台通知”
There are too many settings that need to be set right for it to work. I'll try to dissect them and make it easier to understand.
有太多设置需要正确设置才能正常工作。我会试着剖析它们,让它更容易理解。
Overall, several things are important.
总的来说,有几件事很重要。
- the overall difference between a silentand usernotification
- different types of usernotifications
- how a remote notification, i.e. the payload, is configured from your server
- how to enable push notifications and remote notifications from background modes on your project
- how to register your token with APNs for remoteand silentnotifications and APNs architecture
- how to request permission for usernotifications
- enabling 'background app refresh' and 'notifications' from the device
- what is
content-available
- understanding that the iOS is upstreamto your app when it comes to receiving a remote notification
- what happens when the OS receives notifications when the app has been user-terminated
- A note on reliability and APNs architecture
- 无声通知和用户通知之间的总体差异
- 不同类型的用户通知
- 如何从您的服务器配置远程通知,即有效负载
- 如何从项目的后台模式启用推送通知和远程通知
- 如何使用 APNs 注册您的令牌以进行远程和静默通知以及 APNs 架构
- 如何请求用户通知的权限
- 从设备启用“后台应用程序刷新”和“通知”
- 什么是
content-available
- 了解在接收远程通知时iOS 位于您的应用程序的上游
- 当应用程序被用户终止时操作系统收到通知时会发生什么
- 关于可靠性和 APNs 架构的说明
I highly recommend everyone to watch the first 7 minutes of: WWDC 2015: What's new in Notifications. From there, the presenter mentions that there are 2 major types of notifications:
我强烈建议大家观看以下内容的前 7 分钟:WWDC 2015: What's new in Notifications。从那里,演示者提到有两种主要类型的通知:
Silent Notifications
静默通知
They happen in the background, hence you never see any alert/badge/sound. Things get downloaded withoutyou knowing about them.
它们发生在后台,因此您永远不会看到任何警报/徽章/声音。东西在你不知情的情况下被下载。
iOS 11 bug
See here. iOS 11 initial releases were buggy for silent notifications. Make sure you have the latest version for your testing, otherwise it may not work
iOS 11 错误
见这里。iOS 11 初始版本在静默通知方面存在缺陷。确保你有最新版本的测试,否则它可能无法工作
User Notifications
用户通知
As the name says, it has something to do with the user. That is, the user will see an alert/badge or hear a sound. It has 2 types.
顾名思义,它与用户有关。也就是说,用户将看到警报/徽章或听到声音。它有2种类型。
Local Notifications
本地通知
A Local Notification can be triggeredin 3 different ways:
可以通过3 种不同方式触发本地通知:
UNLocationNotificationTrigger
: You see an alert when you're close to a Walmart store.UNTimeIntervalNotificationTrigger
: e.g. You see an alert every 10 minutes.UNCalendarNotificationTrigger
like December 1st 1:00PM 2017.
UNLocationNotificationTrigger
:当您靠近沃尔玛商店时,您会看到警报。UNTimeIntervalNotificationTrigger
:例如,您每 10 分钟就会看到一次警报。UNCalendarNotificationTrigger
比如 2017 年 12 月 1 日下午 1 点。
Remote Notifications
远程通知
They are similar to localNotifications but they are triggered from the server, e.g. a WhatsApp message that has a From field (Mom) and a body field (I love you!).
它们与 localNotifications 类似,但它们是从服务器触发的,例如具有 From 字段(妈妈)和 body 字段(我爱你!)的 WhatsApp 消息。
Token registration and APNs architecture:
令牌注册和 APNs 架构:
To receive a silent or remote notification, you need to register for a token using:
要接收静默或远程通知,您需要使用以下方法注册令牌:
application.registerForRemoteNotifications()
Registering does NOT require user permission. This makes silent notifications to become seamless. See this moment of the WWDC video
注册不需要用户许可。这使得静默通知变得无缝。观看WWDC 视频的这一刻
Silent notifications are enabled by default. The user does not need to approve your -- does not give permission to your app to use them, and you can just start using them without asking the user for permission.
默认情况下启用静默通知。用户不需要批准您的 -- 不授予您的应用程序使用它们的权限,您可以直接开始使用它们而无需征得用户许可。
RememberAPNs is delivered to your users by APNs and not by your server. So your iOS code must send this token to your server. So the server can associate a given device token with user. When you want to push to a certain user, your server just tells APNs to send a payload to a specific token. What's important to understand is that your server and APNs are two different things
请记住,APN 是由 APN 而非服务器提供给您的用户的。因此,您的 iOS 代码必须将此令牌发送到您的服务器。因此服务器可以将给定的设备令牌与用户相关联。当你想推送给某个用户时,你的服务器只是告诉 APNs 将有效负载发送到特定的令牌。重要的是要了解您的服务器和 APNs 是两个不同的东西
The flow of it looks like this:
它的流程如下所示:
- server/provider sends a payload to APNs
- APNs sends a notification to all target devices of a given account. e.g. your iPhone, Mac could both receive notifications for emails/messages.
- Then your iPhone/Mac will deliver that message to the app. APNs doesn't directly send messages to your app. It sends it to the device. Then the iOS sends it to your app.
- 服务器/提供商向 APNs 发送有效载荷
- APNs 向给定帐户的所有目标设备发送通知。例如,您的 iPhone、Mac 都可以接收电子邮件/消息通知。
- 然后你的 iPhone/Mac 会将该消息传递给应用程序。APNs 不会直接向您的应用发送消息。它将它发送到设备。然后 iOS 将其发送到您的应用程序。
For more on this see docs APNs Overviewand Sending Notification Requests to APNs
有关更多信息,请参阅文档APNs 概述和向APNs发送通知请求
To be able to showbadges/alerts/sounds, you need to request permissionfrom the user:
为了能够显示徽章/警报/声音,您需要向用户请求许可:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
guard error == nil else {
//Display Error.. Handle Error.. etc..
return
}
if granted {
//Do stuff here..
//Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
application.registerForRemoteNotifications()
}
else {
//Handle user denying permissions..
}
}
Question:Do I need to request access once for local notifications and once for remote notifications?
问题:我需要为本地通知请求访问一次,为远程通知请求访问一次吗?
No. Just write the snippet above and it will request access for bothremote and local.
号只写上面的代码片段,它会要求访问两个远程和本地。
Now let's get to the tricky part :D
现在让我们进入棘手的部分:D
Xcode Project + iPhone Settings
Xcode 项目 + iPhone 设置
Do I need to enable something to receive silent notifications?
我需要启用某些东西才能接收静默通知吗?
- You must enable Push Notificationsfrom your Xcode capabilities:
- 您必须从 Xcode 功能启用推送通知:
If you don't enable this, your app won't receive a token. And without a token, the server doesn't recognize you.
如果您不启用此功能,您的应用将不会收到令牌。如果没有令牌,服务器将无法识别您。
- To be able to download stuff from the background, you need to enable: remote notificationsfrom background modes.
- 为了能够从后台下载东西,您需要启用:来自后台模式的远程通知。
To enable backgroundModes, you can do it eitherusing your plistor Xcode capabilities.
要启用backgroundModes,你可以把它做要么用你的plist或Xcode的能力。
The reason you can do it either way is because: plist is closer to your code and is the old way, perhaps it's there for legacy support. Xcode capabilities is the newer, easy way.
您可以采用任何一种方式进行操作的原因是: plist 更接近您的代码并且是旧方式,也许它是用于遗留支持的。Xcode 功能是更新、简单的方法。
plist:
列表:
Item 0 is just an index, it's not the key of a dictionary (something you normally see in plist), the UIBackgroundModes is an array
of Strings. The strings must only come from an accepted valuefrom the UIBackgroundModes Array.
Item 0 只是一个index,它不是字典的键(你通常在 plist 中看到的东西), UIBackgroundModes 是一个array
字符串。该字符串必须只来自一个允许值从UIBackgroundModes阵列。
Xcode Capabilities:
Xcode 功能:
Check the Remote Notification
in Xcode under background modesas below:
检查Remote Notification
在Xcode的背景下模式如下:
If you don't do any of the above, then toggling off notifications with:
如果您不执行上述任何操作,请使用以下命令关闭通知:
will kill Remote & Local Notifications
将杀死远程和本地通知
However, if you doenable background app refresh from plist or Xcode capabilities, then even with notifications turned off for the app, you will still receive silent notifications!
但是,如果您确实从 plist 或 Xcode 功能启用了后台应用程序刷新,那么即使关闭了应用程序的通知,您仍然会收到静默通知!
If the user wants to disable silent notifications, he would have to disable both notifications and disable 'background app refresh' for your app / across the system. To disable 'background app refresh' across your system, you have to do this:
如果用户想要禁用静默通知,他必须禁用这两个通知并禁用您的应用程序/整个系统的“后台应用程序刷新”。要在整个系统中禁用“后台应用程序刷新”,您必须执行以下操作:
Why am I saying all this? To explain to you that settingsof silent and push notifications are different for the user and the restrictions for sending them are different. For more, see this momentfrom the WWDC video. See here instead(previous link was dead):
我为什么要说这一切?向您说明静默和推送通知的设置对用户来说是不同的,发送的限制也不同。有关更多信息,请参阅WWDC 视频中的这一刻。请参阅此处(之前的链接已失效):
Silent notifications are enabled by default.
The user does not need to approve your does not give permission to your app to use them, and you can just start using them without asking the user for permission.
But silent notifications are the mechanism behind background app refresh.
At any point you know that the user can go in settings and disable them.
So you can't depend on them always being available.
You don't know if the user the turn them off, and you are not getting a notification anymore.
This also means that silent notifications are delivered with the best effort.
That means that when the notification arrives on the user's device, the system is going to make some choices.
It's going to use different signals from the device and from the user behavior, like power or the time of day to decide when it is a good time to deliver the notification and launch your app.
It may try to save battery or it may try to match the user behavior and make the content available when the user is more likely to use it.
默认情况下启用静默通知。
用户不需要批准您没有授予您的应用程序使用它们的权限,您可以直接开始使用它们而无需征得用户许可。
但是静默通知是后台应用程序刷新背后的机制。
在任何时候,您都知道用户可以进入设置并禁用它们。
因此,您不能依赖它们始终可用。
您不知道用户是否关闭它们,并且您不再收到通知。
这也意味着会尽最大努力传递静默通知。
这意味着当通知到达用户的设备时,系统将做出一些选择。
它将使用来自设备和用户行为的不同信号,例如电源或一天中的时间来决定何时是发送通知和启动应用程序的好时机。
它可能会尝试节省电池电量,或者可能会尝试匹配用户行为并在用户更有可能使用内容时使其可用。
Also see here.
另请参阅此处。
CAVEAT:Even if you disable app background refresh and disable allow notifications, you can still receive silent notifications if your app is in FOREGROUND. If your app is in background, they won't be delivered.
警告:即使您禁用应用程序后台刷新并禁用允许通知,如果您的应用程序处于前台,您仍然可以收到静默通知。如果您的应用程序在后台运行,它们将不会被交付。
Do I need to enable something to receive remote notifications?
我是否需要启用某些东西才能接收远程通知?
You just need to enable Push Notificationsfrom your Xcode capabilities:
您只需要从 Xcode 功能启用推送通知:
If you don't enable this, your app won't receive a token. And without a token, the server doesn't recognize you.
如果您不启用此功能,您的应用将不会收到令牌。如果没有令牌,服务器将无法识别您。
APNs Payload structure
APNs 有效载荷结构
Curious... Can you tell me what should my payload look like?
好奇……你能告诉我我的有效载荷应该是什么样的吗?
I highly recommend you see Apple§documentation. It's very clear AND ALSO SEE Sending Notification Requests to APNs. Basically platform makes an HTTP/2 call to APNs and sends the desired payload. Sending the correct headers is critical otherwise your notifications are not delivered to the devices!
我强烈建议您查看 Apple §文档。非常清楚,另请参阅向 APNs 发送通知请求。基本上,平台对 APN 进行 HTTP/2 调用并发送所需的有效负载。发送正确的标头至关重要,否则您的通知不会发送到设备!
Thanks, but can you just tell me the important parts?
谢谢,但你能告诉我重要的部分吗?
uhhmm... OK, but just so you know this is from the link I just said:
嗯......好吧,但你知道这是来自我刚刚说的链接:
For Silent Notificationsthere are two criterions:
对于静默通知,有两个标准:
- The payload's
aps
dictionary must include thecontent-available
key with a value of1
. - The payload's
aps
dictionarymust not contain thealert
,sound
, orbadge
keys.
- 负载的
aps
字典必须包含content-available
值为的键1
。 - 有效载荷的
aps
字典中不得包含alert
,sound
或badge
键。
A sample payload would look like this:
示例有效负载如下所示:
{
"aps" : {
"content-available" : 1
},
"acme1" : "bar",
"acme2" : 42
}
acme1, acme2 or just some custom data! But for the aps
key, you MUST follow Apple's structure, otherwise it won't mapcorrectly and you won't be able to read data correctly.
acme1、acme2 或只是一些自定义数据!但是对于aps
key,你必须遵循Apple的结构,否则它不会正确映射,你将无法正确读取数据。
For Remote Notifications:
对于远程通知:
You need an alert
key inside your aps
.
您alert
的aps
.
As an example:
举个例子:
{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
There is also a third optionwhich I will discuss further down the answer.
还有第三种选择,我将在答案中进一步讨论。
As for what the fixed aps
and alert
dictionary keys are, see these Apple docs.
至于什么是固定键aps
和alert
字典键,请参阅这些Apple 文档。
OK, got it. What is content-available
?
好的,我知道了。什么是content-available
?
Very simple. It's just a flag that tells your app that you need to wake up and download something because I have content available for download! For more info, see this exact moment.
很简单。它只是一个标志,告诉您的应用程序您需要唤醒并下载某些内容,因为我有可供下载的内容!有关更多信息,请参阅此确切时刻。
By default the content-available
flag is not included, i.e., by default the notifications you send won'ttrigger application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
or do something in your app. It would just show the notification. If you want to wake up the app (to do something in the background), you need to include content-available
and set it to 1
.
默认情况下content-available
不包含该标志,即默认情况下,您发送的通知不会触发application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
或在您的应用程序中执行某些操作。它只会显示通知。如果您想唤醒应用程序(在后台执行某些操作),您需要包含content-available
并将其设置为1
.
§: If you're using Firebase, your payload structure and keys may be slightly different. For example, the key
content-available
is replaced bycontent_available
. For more, see Firebase documentationand also here.
§:如果您使用Firebase,您的负载结构和密钥可能略有不同。例如,键
content-available
被替换为content_available
。有关更多信息,请参阅Firebase 文档以及此处。
I know you told me that I can only download something into my app when I'm using silent notifications, but is there a way that I can also wake my app up in the background AND download something for remote notifications?
我知道您告诉我,我只能在使用静默通知时将某些内容下载到我的应用程序中,但是有没有一种方法可以让我也可以在后台唤醒我的应用程序并下载一些内容以进行远程通知?
Yes, but then similar to the silent notification, you must also set the content-available
flag to 1, so it would know to wake up and download something. Otherwise, it would just pop and alert/badge/sound but won't download anything.
是的,但是与静默通知类似,您还必须将content-available
标志设置为 1,这样它就会知道要唤醒并下载一些东西。否则,它只会弹出并发出警报/徽章/声音,但不会下载任何内容。
IMPORTANT NOTES:
重要笔记:
- If your app has only silent notifications, just enable "push notifications" + "remote notifications" from capabilities and set
content-available
to1
for each payload. - If your app has only remote notifications, just enable "push notifications" from capabilities. There's nothing to do for the
content-available
. - However, if you want your notifications to show an alert/badge/sound and also download something in the background, you must have both "remote notifications" and "push notifications" enabled + set
content-available
to1
.
- 如果您的应用程序只有无声的通知,就能够从功能和设置“推送通知” +“远程通知”
content-available
对1
每个有效载荷。 - 如果您的应用程序只有远程通知,只需从功能中启用“推送通知”。没有什么可做的
content-available
。 - 但是,如果您希望通知显示警报/徽章/声音并在后台下载某些内容,则必须同时启用“远程通知”和“推送通知”并设置
content-available
为1
。
(THIRD OPTION)
(第三种选择)
{
"aps" : {
"content-available" : 1
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
},
"acme1" : "bar",
"acme2" : 42
}
This momentfrom WWDC video mentions the
WWDC 视频中的这一刻提到了
To Quote the Apple Engineer:
引用苹果工程师的话:
Now, you can in a userremote notification, you can set the same content availableflag that you set in silent notifications, and that allows your app to have some time to download the content or update the content that it wants to be displayed so that when the user taps on the notification, your content is available. And the user sees what it does. This is a way to have a silent notification inside a user notifications like a summary.
现在,您可以在用户远程通知中设置与在静默通知中设置的相同 内容可用标志,这允许您的应用程序有一些时间来下载内容或更新它想要显示的内容,以便当用户点击通知时,您的内容可用。用户会看到它的作用。这是一种在用户通知中包含无提示通知的方法,例如摘要。
Notifications and iOS Application life-cycle
通知和 iOS 应用程序生命周期
I'm confused about remote notifications. I thought whenever I get a notification, my app becomes active in the background and downloads something. Can you explain?
我对远程通知感到困惑。我想每当我收到通知时,我的应用程序就会在后台激活并下载一些东西。你可以解释吗?
e.g. at this moment:
例如此时:
Your iPhone has just received a remote notification with a body of "no sender". To receive this, WhatsApp doesn'thave to be running in the background, i.e., you don't need "Remote Notifications" enabled from BackgroundModes. You would still receive the notification even if your app was force-quit or suspended because the process is managed by the OS, not the WhatsApp app. However, if you want to be able to download the actual message or its image/video to WhatsApp (so that once your user opens WhatsApp, the video would be sitting there waiting for the user), well then you need your app to become active. To do so, you need
content-available : 1
and implementapplication(_:didReceiveRemoteNotification:fetchCompletionHandler:)
.Similarly, if you disabled cellular data for an app, you would still receive its notifications. However, by tapping on that notification, the user won'tbe able to make any network requestsfor that app. They would only be able to openthe app.
Or as for another similar scenario, if the server/access point you're connected to has restricted access for, say, WhatsApp, it would still allow you to receive the APNs notifications. However, by tapping on that notification, the user won'tbe able to make any network requestsfor that app. They would only be able to openthe app.
您的 iPhone 刚刚收到一条带有“无发件人”正文的远程通知。要接收此信息,WhatsApp不必在后台运行,即您不需要从 BackgroundModes 启用“远程通知”。即使您的应用程序被强制退出或暂停,您仍然会收到通知,因为该进程是由操作系统管理的,而不是 WhatsApp 应用程序。但是,如果您希望能够将实际消息或其图像/视频下载到 WhatsApp(这样一旦您的用户打开 WhatsApp,视频就会在那里等待用户),那么您需要使您的应用程序处于活动状态. 为此,您需要
content-available : 1
并实施application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
.同样,如果您为某个应用禁用了蜂窝数据,您仍会收到其通知。但是,通过点击该通知,用户将无法对该应用程序发出任何网络请求。他们只能打开应用程序。
或者对于另一个类似的场景,如果您连接的服务器/接入点限制了对 WhatsApp 的访问,它仍然允许您接收 APNs 通知。但是,通过点击该通知,用户将无法对该应用程序发出任何网络请求。他们只能打开应用程序。
CAVEAT:If the app was force-quit by the user, then while you do get the notification for the above-mentioned reasons, you can't do anything to bring the app out of its terminated state automatically (even if you had content-available
set to 1
). None of your delegate methods would be hit. The usermust open the app and only then your delegate methods will be reached.
警告:如果应用程序被用户强制退出,那么当您由于上述原因收到通知时,您无法做任何事情来使应用程序自动退出终止状态(即使您已content-available
设置为1
)。您的任何委托方法都不会被命中。该用户必须打开应用,并且只有那么你的委托方法将达到。
A note on reliability and APNs architecture:
关于可靠性和 APNs 架构的说明:
Although notifications are heavily used to deliver the actual content to the app, they are are somewhat NOT designed to delivercontent to the app. Rather, they are designed to notifythe user that "hey something new has arrived (a 2b message or a 50kb small image, or a 10mb image or a 2 gb video). Open the app if you like. By the way, here's a small piece of it (the actual message itself ifit can fit, the title of the image or a thumbnail shown in the notification, a title of the video or a thumbnail shown in the video". For more, see iOS APNs “best-effort” fallback. To repeat myself, you never download the 40mb attachment sent in the email. You just get notified of its existence. You send just enough (a thumbnail view of the attachment) so that the user is informed of what's new and can decide whether or not they need to open the app for more. When I was new to iOS, I thought you actually send the image/video through the push notification. You don't!
尽管通知被大量用于向应用程序提供实际内容,但它们在某种程度上并不是为向应用程序提供内容而设计的。相反,它们旨在通知用户“嘿,有新东西到了(2b 消息或 50kb 小图像,或 10mb 图像或 2gb 视频)。如果您愿意,请打开应用程序。顺便说一下,这里有一个它的一小部分(实际消息本身,如果它适合的话,图像的标题或通知中显示的缩略图,视频的标题或视频中显示的缩略图”。有关更多信息,请参阅iOS APNs “best-努力”后备. 再说一遍,您永远不会下载电子邮件中发送的 40mb 附件。您只会收到它存在的通知。您只需发送足够的内容(附件的缩略图),以便用户了解新内容并决定他们是否需要打开应用程序以获取更多信息。当我刚接触 iOS 时,我以为您实际上是通过推送通知发送图像/视频的。你没有!
Specifically in the case of silent notifications:
特别是在静默通知的情况下:
When a device receives a background notification, the system may hold and delay the delivery of the notification, which can have the following side effects:
When the system receives a new background notification, it discards the older notification and only holds the newest one.
If something force quits or kills the app, the system discards the held notification.
If the user launches the app, the system immediately delivers the held notification. Pushing Background Updates to Your App docs
APNs sends a limited number of silent notifications—notifications with the content-available key—per day. In addition, if the device has already exceeded its power budget for the day, silent notifications are not sent again until the power budget resets, which happens once a day. These limits are disabled when testing your app from Xcode. See Pushing Background Updates to Your App.
当设备收到后台通知时,系统可能会保留并延迟通知的传递,这可能会产生以下副作用:
当系统收到新的后台通知时,它会丢弃旧的通知,只保留最新的通知。
如果强制退出或杀死应用程序,系统会丢弃保留的通知。
如果用户启动应用程序,系统会立即发送保留的通知。 将后台更新推送到您的应用文档
APNs 每天发送有限数量的静默通知——带有内容可用密钥的通知。此外,如果设备已经超过了当天的功率预算,则在功率预算重置之前不会再次发送静音通知,这种情况每天发生一次。从 Xcode 测试您的应用程序时,这些限制被禁用。请参阅将后台更新推送到您的应用程序。
Even for remote user notifications, the user may be off of internet and this could cause expired content or APNs could throttle you if you're sending notifications too many or too quickly. See here again
即使对于远程用户通知,用户也可能无法上网,这可能会导致内容过期,或者如果您发送通知过多或过快,APN 可能会限制您。再看这里
Long story short the APNs and OS are King and you're beneath it. Hence you cannot rely on it to conform to your every command. Having that said it's super reliable in the sense that you see most messaging apps utilize it successfully.
长话短说,APN 和操作系统是王道,而您却在它之下。因此,您不能依赖它来遵守您的每条命令。话虽如此,但从您看到大多数消息传递应用程序成功利用它的意义上来说,它是超级可靠的。
回答by Juan Curti
The push notification will let the user know that they receive a notification (Showing the notification popup for example). The silent notification will update, but the user won't get notified about it. In any case, you can perform actions when notified with silent, just as if it was a push notification. The only difference is the user will not get notify with the popup notification.
推送通知会让用户知道他们收到了通知(例如显示通知弹出窗口)。静默通知将更新,但用户不会收到有关它的通知。在任何情况下,您都可以在使用静默通知时执行操作,就像推送通知一样。唯一的区别是用户不会收到弹出通知的通知。
The difference is in the payload:
不同之处在于有效载荷:
Push notification:
推送通知:
aps {
content-available: 1
alert: {...}
}
Silent notification:
静默通知:
aps {
content-available: 0
alert: {...}
}
And you have to set in Capabilitiesthe background mode you choose.
您必须在Capabilities 中设置您选择的背景模式。
回答by Bidisha Pyne
Silent push notification reaches device, user does not know anything about the notification but his app gets the notification and app will be given some time to download new content and present it to the user, regardless to the state of the app (i.e. running or not running)
静默推送通知到达设备,用户对通知一无所知,但他的应用程序收到通知,应用程序将有时间下载新内容并将其呈现给用户,无论应用程序的状态如何(即运行与否)跑步)
Remote push notification method is called only when your app is running. If app is suspended or not running, then the system wakes up or launches your app and puts it into the background running state before calling the method. This method is intended for showing the updated content to the user.When this method is called, your app has up to 30 seconds of wall-clock time to perform the download operation and call the specified completion handler block. If the handler is not called in time, your app will be suspended.
远程推送通知方法仅在您的应用程序运行时调用。如果应用程序挂起或未运行,则系统会在调用该方法之前唤醒或启动您的应用程序并将其置于后台运行状态。此方法旨在向用户显示更新的内容。调用此方法时,您的应用程序有最多 30 秒的挂钟时间来执行下载操作并调用指定的完成处理程序块。如果没有及时调用处理程序,您的应用程序将被挂起。
For more technical details, you can go through this links:
有关更多技术细节,您可以通过以下链接: