diff --git a/LocalNotificationBase.xcodeproj/project.pbxproj b/LocalNotificationBase.xcodeproj/project.pbxproj index dd7afac..329874e 100644 --- a/LocalNotificationBase.xcodeproj/project.pbxproj +++ b/LocalNotificationBase.xcodeproj/project.pbxproj @@ -92,7 +92,7 @@ attributes = { LastSwiftUpdateCheck = 1020; LastUpgradeCheck = 1020; - ORGANIZATIONNAME = "Bruno Omella Mainieri"; + ORGANIZATIONNAME = "Rafael Galdino"; TargetAttributes = { AA6671C422B1292D00824252 = { CreatedOnToolsVersion = 10.2.1; @@ -283,13 +283,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = GT48V5L69E; + DEVELOPMENT_TEAM = RAZG7U3SR6; INFOPLIST_FILE = LocalNotificationBase/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = br.com.mackmobile.LocalNotificationBase; + PRODUCT_BUNDLE_IDENTIFIER = Galdineris.LocalNotificationBase; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; @@ -301,13 +301,13 @@ buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_STYLE = Automatic; - DEVELOPMENT_TEAM = GT48V5L69E; + DEVELOPMENT_TEAM = RAZG7U3SR6; INFOPLIST_FILE = LocalNotificationBase/Info.plist; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", ); - PRODUCT_BUNDLE_IDENTIFIER = br.com.mackmobile.LocalNotificationBase; + PRODUCT_BUNDLE_IDENTIFIER = Galdineris.LocalNotificationBase; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/LocalNotificationBase/AppDelegate.swift b/LocalNotificationBase/AppDelegate.swift index 07da379..a1cff38 100644 --- a/LocalNotificationBase/AppDelegate.swift +++ b/LocalNotificationBase/AppDelegate.swift @@ -10,7 +10,7 @@ import UIKit import UserNotifications @UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { +class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { var window: UIWindow? @@ -44,12 +44,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. + application.applicationIconBadgeNumber = 0 } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - + } diff --git a/LocalNotificationBase/Base.lproj/Main.storyboard b/LocalNotificationBase/Base.lproj/Main.storyboard index 0d2afb5..923998a 100644 --- a/LocalNotificationBase/Base.lproj/Main.storyboard +++ b/LocalNotificationBase/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -10,30 +10,235 @@ - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + - - + + - + + diff --git a/LocalNotificationBase/BaseViewController.swift b/LocalNotificationBase/BaseViewController.swift index b3615fb..c672e43 100644 --- a/LocalNotificationBase/BaseViewController.swift +++ b/LocalNotificationBase/BaseViewController.swift @@ -9,26 +9,133 @@ import UIKit import UserNotifications -class BaseViewController: UIViewController { +class BaseViewController: UITableViewController, UITextFieldDelegate, UNUserNotificationCenterDelegate{ + + @IBOutlet weak var titleTextField: UITextField! + @IBOutlet weak var bodyTextField: UITextField! + @IBOutlet weak var triggerTextField: UITextField! + @IBOutlet weak var soundSwitch: UISwitch! + @IBOutlet weak var badgeSwitch: UISwitch! + override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. + + let tap = UITapGestureRecognizer(target: self.view, + action: #selector(UIView.endEditing(_:))) + tap.cancelsTouchesInView = false + self.view.addGestureRecognizer(tap) + } + + @IBAction func copperButton(_ sender: Any) { + + var title : String = self.titleTextField.text ?? "Recuerda-me" + title = title.isEmpty ? "Recuerda-me" : title + var body : String = self.bodyTextField.text ?? "Aunque Tenga Que Emigrar" + body = body.isEmpty ? "Aunque Tenga Que Emigrar" : body + let triggerTime : Double = (self.triggerTextField.text != nil) ? Double(self.triggerTextField.text!) ?? 5 : 5 - @IBAction func remindButton(_ sender: Any) { + + let sound : Bool = self.soundSwitch.isOn + let badge : Bool = self.badgeSwitch.isOn + + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.getNotificationSettings { (settings) in + if settings.authorizationStatus == .authorized { + + let content = UNMutableNotificationContent() + content.title = NSString.localizedUserNotificationString(forKey: title, + arguments: nil) + content.body = NSString.localizedUserNotificationString(forKey: body, + arguments: nil) + + if sound{ + content.sound = UNNotificationSound.default + } + + if badge{ + content.badge = 1 + } + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: triggerTime, + repeats: false) + + let request = UNNotificationRequest(identifier: "copper", + content: content, + trigger: trigger) + + let center = UNUserNotificationCenter.current() + center.add(request) { (error : Error?) in + if let error = error { + print(error.localizedDescription) + } + } + + } else { + print("Impossível mandar notificação - permissão negada") + } + } + } + + @IBAction func silverButton(_ sender: Any) { + silverNotification() + } + @IBAction func goldButton(_ sender: Any) { + silverNotification() + } + + func silverNotification(){ + var title : String = self.titleTextField.text ?? "Recuerda-me" + title = title.isEmpty ? "Recuerda-me" : title + var body : String = self.bodyTextField.text ?? "Aunque Tenga Que Emigrar" + body = body.isEmpty ? "Aunque Tenga Que Emigrar" : body + let triggerTime : Double = (self.triggerTextField.text != nil) ? Double(self.triggerTextField.text!) ?? 5 : 5 + + + let sound : Bool = self.soundSwitch.isOn + let badge : Bool = self.badgeSwitch.isOn + + let repeatAction = UNNotificationAction(identifier: "repeatAction", + title: "Repeat", + options: [.foreground]) + let openAction = UNNotificationAction(identifier: "openAction", + title: "Let's Go", + options: [.foreground]) + + let silverCategory = UNNotificationCategory(identifier: "silverCategory", + actions: [repeatAction, openAction], + intentIdentifiers: [], + options: []) + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.delegate = self + notificationCenter.setNotificationCategories([silverCategory]) notificationCenter.getNotificationSettings { (settings) in if settings.authorizationStatus == .authorized { let content = UNMutableNotificationContent() - content.title = NSString.localizedUserNotificationString(forKey: "Lembre-se", arguments: nil) - content.body = NSString.localizedUserNotificationString(forKey: "Você se lembrou", arguments: nil) - content.sound = UNNotificationSound.default + content.title = NSString.localizedUserNotificationString(forKey: title, + arguments: nil) + content.body = NSString.localizedUserNotificationString(forKey: body, + arguments: nil) + content.categoryIdentifier = "silverCategory" + + if sound{ + content.sound = UNNotificationSound.default + } + + if badge{ + content.badge = 1 + } - let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: triggerTime, + repeats: false) - let request = UNNotificationRequest(identifier: "5seconds", content: content, trigger: trigger) + let request = UNNotificationRequest(identifier: "copper", + content: content, + trigger: trigger) let center = UNUserNotificationCenter.current() center.add(request) { (error : Error?) in @@ -43,5 +150,19 @@ class BaseViewController: UIViewController { } } + func userNotificationCenter(_ center: UNUserNotificationCenter, + didReceive response: UNNotificationResponse, + withCompletionHandler completionHandler: @escaping () -> Void) { + switch response.actionIdentifier { + case "repeatAction": + silverNotification() + break + case "openAction": + performSegue(withIdentifier: "silverSegue", sender: self) + default: + print("Notifications Actions Not Working Properly") + } + } + + } -