Hello,
It's about Xcode 8.3.2 and Swift 3 project.
I'm using UserNotifications, by adding UNUserNotificationCenterDelegate in my AppDelegate classes.
In order to recive the local notifications, I did these codes in my View Controller.swift :
let yes = UNNotificationAction(identifier: "yes", title: "send SOS", options: .foreground)
let no = UNNotificationAction(identifier: "no", title: "ignore", options: .foreground)
let category = UNNotificationCategory(identifier: "cat", actions: [yes, no], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
let content_2 = UNMutableNotificationContent()
content_2.title = "You'r ok?!"
content_2.body = "Alert!!"
content_2.sound = UNNotificationSound.init(named: "SM.mp3")
content_2.categoryIdentifier = "cat"
let trigger_2 = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
let request = UNNotificationRequest(identifier: "any", content: content_2, trigger: trigger_2)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)And also these fonctions in my AppDelegate.swift :
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (allowed, error) in
}
UNUserNotificationCenter.current().delegate = self
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge, .sound])
}
Until here, I can recive the local notifications. And work great.
Problem :
Normally when I am in View controller, I can open the sms interface window easily with these codes :
let controller = MFMessageComposeViewController()
controller.body = "SOS"
controller.recipients = ["11111111111"]
controller.messageComposeDelegate = self
self.present(controller, animated: true, completion: nil)and this function :
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult)
{
self.dismiss(animated: true, completion: nil)
}But here my UNNotificationResponse function is in AppDelegate class, and I want to apply 2 actions, in my AppDelegate.swift for these responses :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
/
if response.actionIdentifier == "yes"
{
print("Envoyer SOS")
} else
{
print("oui ca va")
}
}(Considering that I'll recive these notifications outside of my application, when it's suspending in background.)
1- To open user interface sms windows with selecting "yes" as response.actionIdentifier.
2 - To ignore the notification and rest outside of application by selecting "no" as response.actionIdentifier.
I tested some methods but always either an error or crash.