Member-only story
Notifications, the whole shebang IV using only iOS
I should start this article with a disclaimer, it based on iOS 13, Swift 5 and Xcode 11.x. If you reading this and those numbers look dated, be forewarned.
I should also warn you that notifications, principally remote ones involve Apple’s infrastructure which means you will need an Apple Developers account to use them.
Now if you just got here, you should perhaps go back to part I, part II and part III first, if not read on.
OK in part III we basically covered the installation of IBM’s SwiftJWT library and configured our JSON Web Token. The next step is to build the post itself. Add this code to your RemoteNotifications.swift file in your Noob project.
func postNotification() {...do {
let jwtString = try jwtEncoder.encodeToString(myJWT)
let content = "https://api.sandbox.push.apple.com/3/device/" + tokenvar loginRequest = URLRequest(url: URL(string: content)!)
loginRequest.allHTTPHeaderFields = ["apns-topic": "ch.cqd.noob",
"content-type": "application/json",
"apns-priority": "10",
"apns-push-type": "alert",
"authorization":"bearer " + jwtString]
let session = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue.main)
loginRequest.httpMethod = "POST"
let data = try? JSONSerialization.data(withJSONObject: jsonObject, options:[])
loginRequest.httpBody = data
} catch {
print("failed to encode")
}
}