Swift - Send notification 3 hours before its due - swift3

I have a local notification and I am wondering how I can send that notification 3 hours before midnight.
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = "Test"
content.sound = UNNotificationSound.default()
let triggerWeekly = Calendar.current.dateComponents([.day], from: datePicker.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

Related

WatchOS, SwiftUI: How to send local notifications with a 'View' as body

When creating the XCode project, I selected Apple Watch Application and enabled "Include Notification".
This lead me to have a NotificationView.swift and a NotificationController.swift in my project.
I have filled the NotificationView.swift with the View content i would like to be in my local notification.
In my regular HostingController.swift I would now like to schedule a local notification with the content of NotificationView.swift.
So far, I am using the current code:
let userNotificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "title"
notificationContent.body = "body"
notificationContent.categoryIdentifier = "categoryNameDummy"
let category = UNNotificationCategory(identifier: "categoryNameDummy", actions: [], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
let categories = Set<AnyHashable>([category])
userNotificationCenter.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
userNotificationCenter.add(request) { (error) in
if let error = error {
debugPrint(error)
}
}
I have not made any changes to Info.plist regarding the categoryIdentifier.
What and where do I have to add code to now "catch" this notification and fill it with my custom NotificationView.swift content?
Did you ask the user for permission to send notifications?
UNUserNotificationCenter.current().requestAuthorization(options: [.sound, .alert]) { success, error in
if success {
print("Ok!")
} else if let error = error {
print(error.localizedDescription)
}
}
It only presents a permission alert the first time: once given permission it won't show up again.

Notification Content Extension is not appearing for multiple category name?

I have added Custom Remote Notification Content Extension to my project and added multiple Extension categories into the Notification Content Extension target info.plist file like the following:
added different types of notification action categories for different notifications into the AppDelegate:
func addRichRotificationActions() {
let confirmAction = UNNotificationAction(identifier: "ConfirmAction", title: "Confirm", options: [.foreground])
let cancelAction = UNNotificationAction(identifier: "CancelAction", title: "Cancel", options: [.destructive])
let closeAction = UNNotificationAction(identifier: "CloseAction", title: "Close", options: [.foreground])
let openTicketCategory = UNNotificationCategory(identifier: "OpenTicket", actions: [confirmAction, cancelAction], intentIdentifiers: [], options: [])
let confirmTicketCategory = UNNotificationCategory(identifier: "ConfirmTicket", actions: [closeAction, cancelAction], intentIdentifiers: [], options: [])
let closeTicketCategory = UNNotificationCategory(identifier: "CloseTicket", actions: [], intentIdentifiers: [], options: [])
let cancelTicketCategory = UNNotificationCategory(identifier: "CancelTicket", actions: [], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([openTicketCategory, confirmTicketCategory, closeTicketCategory, cancelTicketCategory])
}
Now I am sending the apns json following way:
For Open tickets getting category name as "OpenTicket":
[AnyHashable("default"): You have a new ticket, AnyHashable("aps"): {
alert = "#8556 - New Booking for Mr. Tomas";
badge = 1;
category = OpenTicket;
"mutable-content" = 1;
sound = default;
}]
For Confirm tickets getting category name as "ConfirmTicket":
[AnyHashable("default"): You have a confirmed ticket, AnyHashable("aps"): {
alert = "#8556 - Ticket Confirmed for Mr. Tomas";
badge = 1;
category = ConfirmTicket;
"mutable-content" = 1;
sound = default;
}]
and so on.
But unfortunately, I am receiving the default notification with different action buttons rather than the custom notification content extension with different actions. I can't able to figure out the problem. How is it possible to get notification content extension with different actions for remote notification?
Just need to make the UNNotificationExtensionCategory as an Array rather than String in the info.plist of Notification Content Extension target.

Upload Stream Requests and UIProgressView, Swift 3

I would like to track the progress of videos uploaded through a stream request with a UIProgressView. Unfortunately, I am not using Alamofire, so I'm not sure if URLSession has this ability. Below is relevant code from my application.
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
let uploadProgress:Float = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
let uploadCell = contentTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! NewContentCell
uploadCell.uploadProgressView.progress = uploadProgress
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
let uploadCell = contentTableView.cellForRow(at: IndexPath(row: 0, section: 0)) as! NewContentCell
uploadCell.uploadProgressView.progress = 1.0
}
didCompleteWithError correctly sets the UIProgressView to indicate that the upload is complete, however, didSendBodyData is returning values greater than 1 through the uploadProgress calculation.
It's my first time utilizing a stream request, so I'm hoping I simply glossed over something that you could help point out. Here is the structure of my request as well for reference.
let request = NSMutableURLRequest(url: NSURL(string: "\(requestUrl)")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBodyStream = InputStream(data: body as Data)
let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)
let dataTask = session.uploadTask(withStreamedRequest: request as URLRequest)
dataTask.resume()
Much thanks for your input and help.
Implementing
public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64)
is the correct way to track stream request progression.
But if you want to now the totalBytesExpectedToSend, you must tell it to the server. So don't forget to set the correct Content-Length header in your request!
Here's the way i'm creating the request:
var request = URLRequest(url: url)
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
request.addValue(String(dataToUpload.count), forHTTPHeaderField: "Content-Length") // <-- here!
request.httpBodyStream = InputStream(data: dataToUpload)
var task = session.uploadTask(withStreamedRequest: request)
task?.resume()
Reading documentation further, figured out that stream objects do not support totalBytesExpectedToSend. It may be a hack, but just using the file's NSData.length feature allows for correct progress tracking. So for stream requests using URLSession, progress can be tracked by using didSendBodyData, with let uploadProgress: Float = Float(totalBytesSent) / Float(mediaSize), where mediaSize is NSData.length.

Swift 3 >> how to pass data to next view that have been received from HTTP POST Request

please can you explain how to pass the json-data
in "let result" to a new view e.g. a textfield?
Here is the related piece of code:
// make http POST request
let uploadTask = session.uploadTask(with: request as URLRequest, from: body?.data(using: String.Encoding.utf8)!){
(data, response, error) in
(response as? HTTPURLResponse)?.statusCode
// URL Object to String
let result = String(data: data!, encoding: String.Encoding.utf8)
}
uploadTask.resume()

How to send json request to server and not wait on response

I am building mobile applications to go with my django based backend. I make a post request in swift like this:
var request: NSMutableURLRequest = NSMutableURLRequest()
var url = "https://webapp.com/makepost/"
url += NSUserDefaults.standardUserDefaults().stringForKey("userPk")!
url += "/"
var err: NSError?
request.URL = NSURL(string: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(JSONObject, options: NSJSONWritingOptions(rawValue:0))
} catch _ {
print ("error")
}
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) {(response, data, error) -> Void in
var query = String(data: data!, encoding: NSUTF8StringEncoding)
query = query!.stringByReplacingOccurrencesOfString("Optional(", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
query = query!.stringByReplacingOccurrencesOfString(")", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
print(query)
//update ui
}
As soon as I make this post it creates the necessary models in django by reading the jsonObject.
The response doesn't matter and could take long as I'm notifying other users via FCM.
This is what I'm trying to do:
Make a post request.
Ignore the response.
Update the UI immediately after I make the post request.
How should I achieve this?
like so
//set request variable here
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) {(response, data, error) -> Void in
var query = String(data: data!, encoding: NSUTF8StringEncoding)
}
sleep(2)
// wait two seconds for models to create in backend
dispatch_async(dispatch_get_main_queue()) {
self.refresh(self.refreshButton)
//refresh ui here with method that fires another get request
}
}