静默推送消息,如果应用程序在后台并拔掉电源,则无法做出反应 [英] Silent push message, can´t react if app is in background and unplugged

查看:20
本文介绍了静默推送消息,如果应用程序在后台并拔掉电源,则无法做出反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对静默推送消息做出反应.我在我的 plist 文件中为这两种后台模式注册了我的应用.

I am trying to react on silent push messages. I registered my app for the two background modes in my plist file.

我将以下 json 发送到我的应用程序:

I am sending the following json to my app:

{
"aps": {
     "content-available": 1
},
"Bla": "Blub"
}

如果我的应用程序在前台,则一切正常.当我收到推送时,我在我的代码中执行了一个函数.如果我的应用程序在后台并插入我的 Mac 或 ac,一切都很好...

If my app is in foreground, everything works. As I am receiving the push I execute a function in my code. If my app is in background and plugged to my Mac or ac, everythings fine as well... But if I unplug my device, the device is not getting or at least my function is not called anymore...Why is that?

推荐答案

我理解静默通知的工作方式是它们可以被发送,系统可能会收到它们,但可能不会根据不同的因素对它们采取行动.

The way I understand that silent notifications work is that they can be sent, the system may receive them, but may not act upon them depending on different factors.

无声通知用于在同样节能的设备上提供多任务处理的错觉.我的理解是,他们的系统会使用一些因素来确定是否应该将无提示通知传递给应用,其中包括:

A silent notification is used to give the illusion of multi-tasking on a device that is power efficient as well. My understanding is that there are a few factors they system uses to determine whether it should pass along a silent notification to an app, these include:

  • 当前网络状况
  • 当前功率水平
  • 应用程序使用.
  • 用户选择
  • 可能耗电.

在你的情况下,静默通知在前台工作时,因为你是最重要的应用程序.用户可能正在执行不立即涉及内容的任务.但内容更新可能会有用.例如使用 Facebook,我可能会在朋友墙上写一篇文章,我的动态中有新故事.很高兴在我做其他事情的同时更新它.

In your case the silent notification is working when in the foreground because you are the foremost application. The user could be doing a task which does not involve the content right away. But the content update could be useful. For example with Facebook, I may be writing a post on a friends wall, there are new stories for my feed. It is nice to have that updated whilst I am doing something else.

对于连接到计算机的情况 - 设备正在充电,执行后台任务不会消耗设备电池.

For the case of being connected to your computer - The device is charging doing the background task is not going to be a drain on the device battery.

要提供有关因素的更多详细信息:

To give more details for the factors:

网络条件

  • 如果设备使用 Wi-Fi,则更有可能允许网络通话,因为这不像使用蜂窝数据那样耗电.

当前功率水平

  • 这也与网络条件有关.
  • 是否在相关无线电已经打开的时候收到通知?
  • 这也可以根据设备历史记录与当前位置相关联.如果该位置被确定为家",则可以假设该设备靠近电源.
  • 时间也可能是其中的一个因素.例如设备开始充电的平均时间是多少,是否关闭等."
  • 设备是否已连接到电源?

应用程序使用

  • 用户使用您的应用程序的频率如何?使用模式是什么?
  • 是否在允许应用更新其内容的时间收到通知?一个可能的例子是,用户可能会在接下来的 10 分钟到 1 小时内使用该应用程序.更新内容可能是有意义的.但如果用户很可能在接下来的 2 分钟或 2 小时内使用该应用程序.它可能会推迟此操作,因为它可能很快会被用户启动或不会很快被使用,这是一种资源浪费.

用户选择

  • 用户可以随时选择停止后台数据刷新.
  • 用户可以选择不允许您的应用使用蜂窝数据.
  • 虽然超出了您的问题范围,但我认为应该注意的是,不应假定无声通知总是会出现,也不应该是您申请的关键部分.

可能的耗电

  • 与其他因素相比,我认为这不存在,但需要牢记.
  • 在您执行此后台任务时,过去的功耗情况如何?
  • 如果你是一个好公民,尽可能节能等,你可能更有可能被要求更新.
  • 但是,如果您在执行此任务时未在系统视图中尽可能节能,则您可能不会收到尽可能多的通知.

据我所知,这些因素都不是确定的、证明的或陈述的.我根据 WWDC 谈话、以前的后台任务限制和我自己的假设建议它们作为可能的原因.但我希望它们能帮助您了解为什么您的无提示通知在某些情况下有效,而在其他情况下无效.

These factors are not definitive, proven or stated from what I can tell. I am suggesting them as possible reasons based on WWDC talks, previous background task restrictions and my own assumptions. But I hope they help give an idea why your silent notifications are working in certain instances and not in others.

这篇关于静默推送消息,如果应用程序在后台并拔掉电源,则无法做出反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆