Issue
I am creating app which, among other things, allows users to send messages to each other. I have the messages stored locally in an sqlite database (constructed with FMDB) and it works as intended. However, I am running into a bit of an issue with figuring out how to send the messages between users. What I would like to do is to create momentary objects of the texts in my main database (parse-server) containing the sender's ID, the receiver's ID, and the actual message. This itself is not difficult. What is challenging is figuring out how to allow the receiver's app to recognize when a message has been sent to them and then load that message. I can very easily perform the loading of the message in the viewDidLoad()
:
func fetchNewestMessageQuery(){
//pull the message
tabelView.reloadData() //reload the tableView's data to
//populate it with the newest message
}
override func viewDidLoad() {
super.viewDidLoad()
fetchNewestMessageQuery()
//save the new message to the SQLite database
}
This, however, means that the user only ever gets the message when they open the page. How do I basically call my fetchNewestMessageQuery()
at any time the database creates a new message object to then send a notice to the app (which may not even be open on the user's phone) to run the query (which is the way basically all message apps work)? I have always been used to only running actions/functions on predictable events in XCode... viewDidLoad()
, viewDidAppear()
, UIButton
presses, etc... and have no idea where to start on functions running without prompt from the app itself.
Solution
You can use silent push notifications where the content-available
parameter is sent, and it will be delivered even if the user has disabled push notifications for your app (alerts, really). Your app will even be woken up if it's not already running, and it will be given 30 seconds to process the notification (as long as the user didn't force quit the app).
However, they must have Background App Refresh (requires iOS 7+) enabled for your app. The good news is Background App Refresh will be on by default.
See Configuring a Silent Notification.
You'll need to setup a server to send push notifications.
Alternatively, establish an outgoing network connection of your own to your server, which is basically what iOS does to the Apple Push Notification service.
Answered By - Marcus Adams Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.