简体   繁体   English

iOS / Swift:当应用程序处于后台时,无需推送通知即可接收来自数据流服务的回调

[英]iOS / Swift : Receive callbacks from data streaming service without push notifications while app is in background

The Background Information: 背景信息:

I am writing an application that uses the PubNub framework to directly couple one Raspberry Pi with one iPhone, for each individual user. 我正在编写一个应用程序,该应用程序使用PubNub框架为每个个人用户直接将一个Raspberry Pi与一个iPhone耦合。 The Raspberry Pi uses an Arduino as a slave to gather analog data, then uses the PubNub network to publish that data to the one and only iPhone that is on the same channel. Raspberry Pi使用Arduino作为从设备来收集模拟数据,然后使用PubNub网络将该数据发布到同一通道上的唯一一个iPhone。 With the data that the iPhone receives, it determines (locally) if the user needs to be alerted. 使用iPhone接收的数据,它可以(本地)确定是否需要提醒用户。 Then, when the user is alerted, they have the ability to adjust the current state of the Raspberry Pi by sending data (settings) back to the Pi, which will then resolve the problem that warranted a user alert. 然后,当用户收到警报时,他们可以通过将数据(设置)发送回Pi来调整Raspberry Pi的当前状态,这将解决保证用户警报的问题。

The Problem: 问题:

When the iPhone app goes into the background, the messages sent by the Raspberry Pi that the iPhone should be receiving will no longer be caught by the application, and therefor the user is no longer notified when they need to be. 当iPhone应用程序进入后台时,该应用程序将不再捕获Raspberry Pi发送的iPhone应该接收的消息,因此不再需要通知用户。 The first obvious solution would be to move the logic of what warrants an alert to the Raspberry Pi itself, so that it can use Apple Push Notifications to send those alerts to the user. 第一个显而易见的解决方案是将需要发出警报的逻辑转移到Raspberry Pi本身,以便它可以使用Apple Push Notifications将这些警报发送给用户。 However, the problem with this is that I am also trying to track whether or not the Raspberry Pi connection has timed out, so that if some unexpected disconnect occurs, the user is notified of that problem as well. 但是,与此有关的问题是,我还试图跟踪Raspberry Pi连接是否超时,以便在发生意外断开连接时,也会向用户通知该问题。 This logic obviously can not be on the Raspberry Pi itself, because if a disconnect occurs, then it will not be able to push the notification to the iPhone itself. Raspberry Pi本身显然不能采用这种逻辑,因为如果断开连接,它将无法将通知推送到iPhone本身。 Having a middle man server of sorts to monitor the state of the device would seem like a logical solution... however I do not want anything other than the iPhone and Raspberry Pi involved in the data transmission and handling, which is clearly the entire motivation behind using PubNub in the first place (within the scope of my application). 拥有一个可以监视设备状态的中间人服务器似乎是一个合理的解决方案……但是,除了数据传输和处理过程中涉及的iPhone和Raspberry Pi之外,我什么都不想要,这显然是整个动机首先使用PubNub(在我的应用程序范围内)。

This is not a PubNub specific problem. 这不是PubNub的特定问题。 I only cite them to paint a clearer picture. 我只引用它们来画出清晰的画面。 Also worth noting is that I do not want to fake a location service in order to get iOS to grant continuous background permissions. 另外值得注意的是,我不想为了获得iOS授予连续后台权限而伪造位置服务。 This is a lazy and sloppy solution with undesired overhead. 这是一个懒惰且草率的解决方案,具有不希望的开销。

The Question: 问题:

How do I receive (or request) a short string every 15 to 30 seconds in the background to determine if I should throw an alert. 如何在后台每隔15到30秒接收(或请求)一个短字符串,以确定是否应该发出警报。 This has to be achievable. 这必须是可以实现的。 Based on my research and reading of apple documentation, it is clear to me that many people will try to respond with "it isn't possible." 根据我的研究和对Apple文档的阅读,对我来说很明显,很多人会尝试以“不可能”做出回应。 I do not welcome this answer . 我不欢迎这个答案 I am here to find a solution that I could not previously find, or that has not previously been proposed. 我在这里找到我以前找不到或以前没有提出过的解决方案。 I need an intelligent engineer to propose a genuine solution or workaround to my problem. 我需要一个聪明的工程师来提出真正的解决方案或解决我的问题的方法。

I sincerely thank the champion engineer in advance. 我衷心感谢冠军工程师。

@JonW I read your question, and some of the comment until I got to the TL;DR point :) The short answer is that silent push notifications might be the answer to one of your problems.The other might be PubNub Presence Webhooks . @JonW我阅读了您的问题,以及在到达TL; DR点之前的一些评论:)简短的回答是, 静默推送通知可能是您遇到的一个问题的答案。另一个可能是PubNub Presence Webhooks But I would agree with @Paulw11 that a server is the best practice and ultimately, PubNub BLOCKS will be your solution. 但是我同意@ Paulw11的观点,认为服务器是最佳实践,最终, PubNub BLOCKS将是您的解决方案。

Background Processing with Apple Silent Push Notifications 苹果静音推送通知的后台处理

To do some short background processing, you can have your RPi publish a message that includes a push notification (as I believe you are already doing). 要进行一些简短的后台处理,您可以让RPi发布包含推送通知的消息(因为我相信您已经在这样做)。 But this push msg should be a silent push notification . 但是此push msg应该是无提示的推送通知 The docs say this: 文档说:

When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. 当收到静默通知时,iOS会在后台唤醒您的应用程序,以便您可以从服务器获取新数据或进行后台信息处理。

... ensure there is no alert, sound, or badge payload in the aps dictionary ...确保aps词典中没有警报,声音或徽章有效载荷

The full details are at the link I just provided but here is a sample message payload that you would publish on PubNub with the proper aps format. 有关详细信息,请参见我刚刚提供的链接,但这是示例消息有效负载,您可以使用正确的aps格式将其发布在PubNub上。

{
    "pn_apns": {
        "aps": {
            "content-available": 1,
            "data": {
                "temperature": "55",
                "humidity": "42%"
            }
        }
    },
    "data": {
        "info": "This is the full realtime message.",
        "temperature": "55",
        "humidity": "42%"
    }
}

WARNING: The silent push notifications are only effective if the app is not in a kill state. 警告:无提示推送通知仅在应用程序未处于终止状态时才有效。 In other words, it must be idle in background - not running, but idle. 换句话说,它必须在后台处于空闲状态-不在运行,而是处于空闲状态。 If you force kill the app (swipe up from recent apps list by double tap Home button) or do not start the app after device has been powered off then on again, then silent pushes will be ignored. 如果您强行终止应用程序(通过双击“主页”按钮从最近的应用程序列表中清除)或在设备关闭电源后又重新启动后不启动应用程序,则无声推送将被忽略。

See this Badge Count Demo as a template for getting started. 请参阅此徽章计数演示作为入门模板。 But as @Paulw11 said, regular push notifications every 30 seconds is not a good idea. 但是正如@ Paulw11所说,每30秒定期推送通知不是一个好主意。 You should be sending your updates to a server that can send a push notification to the iPhone app when it is necessary to take action. 您应该将更新发送到服务器,该服务器可以在需要采取措施时将推送通知发送到iPhone应用程序。

Offline Notification with PubNub Presence Webhooks 使用PubNub Presence Webhooks进行脱机通知

Going further down the server process best practice, you can have your server monitor the presence of the RPi on the channel. 进一步了解服务器过程的最佳实践,您可以让服务器监视通道上RPi的存在 If the RPi ever leaves the channel by explicit unsubscribe from channel ( leave event) or by network disconnect ( timeout event), then a message can be POST ed to your server REST endpoint (that you provide us to configure on your PubNub keys). 如果RPI一旦脱离由通道明确退订通道( leave事件)或网络断开连接( timeout事件),那么可将消息POST编到你的服务器REST端点(您提供给我们来配置你的PubNub键)。 If either of these events happens, then you can publish a message (silent push payload included) to your iPhone app to take appropriate action. 如果这些事件之一发生,那么您可以向iPhone应用程序发布一条消息(包括静默的推送有效负载)以采取适当的措施。

PubNub BLOCKS - No Server Required (Look mom, no server!) PubNub块-不需要服务器(妈妈,没有服务器!)

So you say you want to avoid using a server. 因此,您说您要避免使用服务器。 With PubNub BLOCKS , you will be able to avoid using your own server - instead, you will use PubNub servers . 使用PubNub BLOCKS ,您将能够避免使用自己的服务器-而是使用PubNub服务器

I won't into too much details here, but you will be able to write a small bit of JavaScript in a BLOCK that can determine if a push notification needs to be sent or not and much much more. 我在这里不做过多介绍,但是您将能够在BLOCK中编写少量JavaScript,该JavaScript可以确定是否需要发送推送通知,并且还有更多其他内容。

Summary 摘要

For now, I think your prototype with silent push is good to flesh out your use case. 就目前而言,我认为通过静默推送的原型可以充实您的用例。 But ultimately, you need to have an always on process that can determine when it is necessary to send a push notification. 但是最终,您需要有一个始终在线的流程,可以确定何时需要发送推送通知。 While your iPhone app is active, it can receive the realtime messages from RPi, but when in background, getting a silent push every 30 seconds is not ideal and possibly not allowed by Apple. 当您的iPhone应用程序处于活动状态时,它可以从RPi接收实时消息,但是在后台运行时,每30秒进行一次无声推送并不理想,并且Apple可能不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM