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

╄→尐↘猪︶ㄣ 提交于 2020-01-03 03:34:06

问题


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. 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. With the data that the iPhone receives, it determines (locally) if the user needs to be alerted. 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.

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. 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. 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. 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. 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).

This is not a PubNub specific problem. 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. 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. 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." 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.


回答1:


@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. But I would agree with @Paulw11 that a server is the best practice and ultimately, PubNub BLOCKS will be your solution.

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). But this push msg should be a silent push notification. 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.

... ensure there is no alert, sound, or badge payload in the aps dictionary

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.

{
    "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. 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.

Offline Notification with 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. 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 POSTed to your server REST endpoint (that you provide us to configure on your PubNub keys). If either of these events happens, then you can publish a message (silent push payload included) to your iPhone app to take appropriate action.

PubNub BLOCKS - No Server Required (Look mom, no server!)

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.

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.

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.



来源:https://stackoverflow.com/questions/35761950/ios-swift-receive-callbacks-from-data-streaming-service-without-push-notific

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!