Thank you for your answer. I made it work
In app delegate:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Here return .newData after you checked if you have any new data, or .noData in case of nothing to update.
// A request to the server can be made to check if new data are available for the app
completionHandler(UIBackgroundFetchResult.newData)
}
For those who are using firebase push notification, I use this: ["to" : token, "priority": "normal", "content_available": true] for silent notification
Post
Replies
Boosts
Views
Activity
yes they are push notifications.
So yes, my question is, how do I set things up to run my app in the background when the notification arrives?
Thank you
Thank you for your answer.
I realized I didn't explain myself good enough, sorry.
I'm not trying to continue a request in the background.
In the normal situation, when the user is on the app and received a notification, my app will automatically send a request to the user's smart home device. This is working.
Now, my goal is when the user doesn't use the app, so this one is not opened (background), when the phone receive the notification from my app, I want to send the same request to the user's smart home device.
So when the app is in background I still want to be able to send a request based on the phone receive my app notification.
Is it something possible?
Here a code I implemented and working:
class NSURLSessionPinningDelegate: NSObject, URLSessionDelegate {
let certFileName = "phillips-hue-cert"
let certFileType = "pem"
func urlSession(_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
if let serverTrust = challenge.protectionSpace.serverTrust {
var secresult = SecTrustResultType.invalid
let status = SecTrustEvaluate(serverTrust, &secresult)
if(errSecSuccess == status) {
if let serverCertificate = SecTrustGetCertificateAtIndex(serverTrust, 0) {
let serverCertificateData = SecCertificateCopyData(serverCertificate)
let data = CFDataGetBytePtr(serverCertificateData);
let size = CFDataGetLength(serverCertificateData);
let certificateOne = NSData(bytes: data, length: size)
let filePath = Bundle.main.path(forResource: self.certFileName,
ofType: self.certFileType)
if let file = filePath {
if let certificateTwo = NSData(contentsOfFile: file) {
completionHandler(URLSession.AuthChallengeDisposition.useCredential,
URLCredential(trust:serverTrust))
return
}
}
}
}
}
}
completionHandler(URLSession.AuthChallengeDisposition.cancelAuthenticationChallenge, nil)
}
}
Then I replaced UrlSession by:
let session = URLSession(
configuration: URLSessionConfiguration.ephemeral,
delegate: NSURLSessionPinningDelegate(),
delegateQueue: OperationQueue.main)
session.dataTask(with: request) { (data, response, error) in .......
That's working for me. I hope it's a good solution and a secured one. It looks good to me. If anyone as a better solution, I will be happy to hear about it.
Here, a link that explain what it is and how to use it.
Say like you wanted to clean up your database in background to delete old records. First, you have to enable background processing in your Background Modes Capabilities. Then in your Info.plist add the background task scheduler identifier.
Then in 'ApplicationDidFinishLaunchingWithOptions' method register your identifier with the task.
// Downcast the parameter to a processing task as this identifier is used for a processing request
self.handleDatabaseCleaning(task: task as! BGProcessingTask)
}
Do the work that you wanted to perform in the background and put it into the operation queue. In our case, the cleanup function will looks like:
func handleDatabaseCleaning(task: BGProcessingTask) {
let queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
// Do work to setup the task
let context = PersistentContainer.shared.newBackgroundContext()
let predicate = NSPredicate(format: "timestamp < %@", NSDate(timeIntervalSinceNow: -24 * 60 * 60))
let cleanDatabaseOperation = DeleteFeedEntriesOperation(context: context, predicate: predicate)
task.expirationHandler = {
// After all operations are canceled, the completion block is called to complete the task
queue.cancelAllOperations()
}
cleanDatabaseOperation.completionBlock {
// Perform the task
}
// Add the task to the queue
queue.addOperation(cleanDatabaseOperation)
}
Now, when the app goes into the background we have to schedule the background task in BGTaskScheduler.
Okay thank you for your reply and those informations! I’ll try that today. I will let you know
Accidentally close this question
Create new one here: https://developer.apple.com/forums/thread/707602
Thank you for your reply, I attached the full content to this message.
One thing to know is that the api url I'm using won't be the same between each user of my app, because it will connect to a hue bridge, so the IP address will change.
phillips-hue-cert.txt
Here in the attachment the full content of it.
I read the articles you sent, I understand more how TLS is working, but I still don't concretely know how to implement an api request with the certificate. Also something that might be interesting to know is that the url I'll use, will never be the same. It's to connect to hue bridge (so every home will have a hue bridge with a different IP address).
phillips-hue-cert.txt
Thank you for your answer, I will take a look right now at the links you sent me.
Here the link to the certificate: https://developers.meethue.com/develop/application-design-guidance/using-https/
but you need to create a Phillips Hue developer account.
It looks like that:
-----BEGIN CERTIFICATE-----
MIICMjCCAdigAwIBAgIUO7FSLbaxikuXAljzVaurLXWmFw4wCgYIKoZIzj0EAwIw
OTELMAkGA1UEBhMCTkwxFDASBgNVBAoMC1BoaWxpcHMgSHVlMRQwEgYDVQQDDAty
........
........
........
MAoGCCqGSM49BAMCA0gAMEUCIEBYYEOsa07TH7E5MJnGw557lVkORgit2Rm1h3B2
sFgDAiEA1Fj/C3AN5psFMjo0//mrQebo0eKd3aWRx+pQY08mk48=
-----END CERTIFICATE-----