diff --git a/LocalNotificationBase/AppDelegate.swift b/LocalNotificationBase/AppDelegate.swift index 07da379..275d352 100644 --- a/LocalNotificationBase/AppDelegate.swift +++ b/LocalNotificationBase/AppDelegate.swift @@ -40,6 +40,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. + application.applicationIconBadgeNumber = 0 } func applicationDidBecomeActive(_ application: UIApplication) { diff --git a/LocalNotificationBase/Base.lproj/Main.storyboard b/LocalNotificationBase/Base.lproj/Main.storyboard index 0d2afb5..23951db 100644 --- a/LocalNotificationBase/Base.lproj/Main.storyboard +++ b/LocalNotificationBase/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -31,9 +31,134 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LocalNotificationBase/BaseViewController.swift b/LocalNotificationBase/BaseViewController.swift index b3615fb..2bbd810 100644 --- a/LocalNotificationBase/BaseViewController.swift +++ b/LocalNotificationBase/BaseViewController.swift @@ -17,30 +17,7 @@ class BaseViewController: UIViewController { } @IBAction func remindButton(_ sender: Any) { - let notificationCenter = UNUserNotificationCenter.current() - 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 - - let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) - - let request = UNNotificationRequest(identifier: "5seconds", 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") - } - } + editarNotificacao("Teste", "Des teste", "qualquercoisa", 5, 1, true) } } diff --git a/LocalNotificationBase/Configuracao.swift b/LocalNotificationBase/Configuracao.swift new file mode 100644 index 0000000..e058a32 --- /dev/null +++ b/LocalNotificationBase/Configuracao.swift @@ -0,0 +1,55 @@ +// +// Configuracao.swift +// LocalNotificationBase +// +// Created by Fabrício Guilhermo on 13/06/19. +// Copyright © 2019 Bruno Omella Mainieri. All rights reserved. +// + +import UIKit + +class Configuracao: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { + + + @IBOutlet weak var titleNotification: UITextField! + @IBOutlet weak var bodyNotification: UITextField! + @IBOutlet weak var pickerTimeWating: UIPickerView! + @IBOutlet weak var soundSwitch: UISwitch! + @IBOutlet weak var badgeSwitch: UISwitch! + + var elem = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] + + var pickerSelected = "" + + func numberOfComponents(in pickerView: UIPickerView) -> Int { + return 1 + } + + func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { + return elem.count + } + + func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { + return elem[row] + } + + func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { + pickerSelected = elem[row] + } + + @IBAction func doneButton(_ sender: Any) { + let title = self.titleNotification.text as! String + let body = self.bodyNotification.text as! String + let sound = self.soundSwitch.isOn + let badge = self.badgeSwitch.isOn + guard let time = TimeInterval(pickerSelected) else { return } + editarNotificacao(title, body, "", time, badge ? 1 : 0, sound) + } + + override func viewDidLoad() { + super.viewDidLoad() + pickerSelected = elem[0] + self.pickerTimeWating.delegate = self + self.pickerTimeWating.dataSource = self + } +} diff --git a/LocalNotificationBase/editNotification.swift b/LocalNotificationBase/editNotification.swift new file mode 100644 index 0000000..6e119ab --- /dev/null +++ b/LocalNotificationBase/editNotification.swift @@ -0,0 +1,45 @@ +// +// editNotification.swift +// LocalNotificationBase +// +// Created by Fabrício Guilhermo on 13/06/19. +// Copyright © 2019 Bruno Omella Mainieri. All rights reserved. +// + +import UIKit +import UserNotifications + +func editarNotificacao(_ title:String, _ body:String, _ identifier:String, _ time: TimeInterval, _ notificationBall:NSNumber, _ sound:Bool) -> Void { + + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.getNotificationSettings { (settings) in + if settings.authorizationStatus == .authorized { + + // var titulo:String + // var conteudo:String + + let content = UNMutableNotificationContent() + content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil) + content.body = NSString.localizedUserNotificationString(forKey: body, arguments: nil) + content.sound = UNNotificationSound.default + + content.sound = sound ? UNNotificationSound.default : nil + + content.badge = notificationBall + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: time, repeats: false) + + let request = UNNotificationRequest(identifier: "5seconds", 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") + } + } +}