IOS Firebase background fetch
When using application(_:didReceiveRemoteNotification:)
you must always call the completion handler, no matter what. Apples policy is that you call completionHandler(.newData)
if your fetch found new data, completionHandler(.noData)
if your fetch didn't find any new data, and completionHandler(.failed)
if your fetch found new data, but failed while trying to retrieve it.
In your code the completion handler is only called if certain conditions are met. Instead of not calling the completion handler, you should call completionHandler(.failed)
or completionHandler(.noData)
.
Thus, your final code (updated for Swift 3) will be:
func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
var newData = false
if let tabBarController = window?.rootViewController as? UITabBarController,
viewControllers = tabBarController.viewControllers! as [UIViewController]!
{
for viewController in viewControllers {
if let a1 = viewController as? HorariosViewController {
newData = true
a1.interface()
}
}
}
completionHandler(newData ? .newData : .failed) // OR completionHandler(newData ? .newData : .noData)
}
Firebase listener when app is in the background
Devices have their own way of managing background network calls so handling background push notifications like this will not work on real devices. The observers are removed from memory shortly after the app goes into the background. I was able to solve this by creating a Node.js server which observes the notifications
table and handles push notifications using the Firebase-Messenger framework. It was actually pretty easy. I used this blog post as my guide:
Sending notifications between Android devices with Firebase Database and Cloud Messaging
This method will work however in the simulator (but not on real devices) so long as you pass the object containing the observer (the app delegate in this case) to background thread call like this:
func applicationWillResignActive(_ application: UIApplication) {
NotificationCenter.default.addObserver(self,
selector: #selector(self.observeNotificationsChildAddedBackground),
name: notificationBackgroundProcessName,
object: self)
}
Flutter, Firebase ios, cloud message doesn't show when app in background, only if app in foreground
Well, the solution was quite simple, and a mistake on my behalf.
As the documnents pub.dev state:
Generate the certificates required by Apple for receiving push notifications following this guide in the Firebase docs. You can skip the section titled "Create the Provisioning Profile".
Here is the link:
https://firebase.google.com/docs/cloud-messaging/ios/certs
I forgot to do these steps. After doing this, it worked
Flutter IOS Workmanager Background Fetch Never Called in Production
Ok. For future people reading this with the same issue, this does 'eventually' work.
After having the app open in the background for ~48hrs, I finally got a background task to run. This is by no means a complete success as it is not really at the frequency desired, but this implementation does 'work' none the less.
Just run in debug mode to confirm and be very patient.
Related Topics
Swift Language Multicast Delegate
Routing Between Points with Mapbox
Unrecognized Selector Sent to Instance Swift 3
Can't Set Calayer's Border Color
How to Use Urlsession with Proxy in Swift 3
Uicollectionview Cells with Images Inside Uitableview Prototype
@Fetchrequest + Case-Insensitive Sorting - Swiftui & Coredata
Horizontal Paging Uicollectionview with Automatic Item Size in a Vertical Stack View
Uitableviewcell Custom Class - Reload Cell Height After Change in Subview Height Constraint
Uitextfield Starting Cursor Position Is Wrong
Running an iOS7 Project Under Xcode 6 - Image Assets Don't Show Up
Programmatically Obtaining the Imei or Udid of an iOS Device
How to Create an Alert in a Swift File Model That Works for Various View Controller
How to Get a Substring from a Specific Character to the End of the String in Swift 4
Uitableviewcell Shows Incorrect Results from Document Folder