diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/LocalNotificationBase.xcodeproj/project.pbxproj b/LocalNotificationBase.xcodeproj/project.pbxproj index dd7afac..c0e0f62 100644 --- a/LocalNotificationBase.xcodeproj/project.pbxproj +++ b/LocalNotificationBase.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 615E408E22B2C295008D1B28 /* BronzeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 615E408D22B2C295008D1B28 /* BronzeViewController.swift */; }; AA6671C922B1292D00824252 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA6671C822B1292D00824252 /* AppDelegate.swift */; }; AA6671CB22B1292D00824252 /* BaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA6671CA22B1292D00824252 /* BaseViewController.swift */; }; AA6671CE22B1292D00824252 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA6671CC22B1292D00824252 /* Main.storyboard */; }; @@ -15,6 +16,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 615E408D22B2C295008D1B28 /* BronzeViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BronzeViewController.swift; sourceTree = ""; }; AA6671C522B1292D00824252 /* LocalNotificationBase.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LocalNotificationBase.app; sourceTree = BUILT_PRODUCTS_DIR; }; AA6671C822B1292D00824252 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; AA6671CA22B1292D00824252 /* BaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseViewController.swift; sourceTree = ""; }; @@ -56,6 +58,7 @@ children = ( AA6671C822B1292D00824252 /* AppDelegate.swift */, AA6671CA22B1292D00824252 /* BaseViewController.swift */, + 615E408D22B2C295008D1B28 /* BronzeViewController.swift */, AA6671CC22B1292D00824252 /* Main.storyboard */, AA6671CF22B1293000824252 /* Assets.xcassets */, AA6671D122B1293000824252 /* LaunchScreen.storyboard */, @@ -135,6 +138,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 615E408E22B2C295008D1B28 /* BronzeViewController.swift in Sources */, AA6671CB22B1292D00824252 /* BaseViewController.swift in Sources */, AA6671C922B1292D00824252 /* AppDelegate.swift in Sources */, ); diff --git a/LocalNotificationBase/AppDelegate.swift b/LocalNotificationBase/AppDelegate.swift index 07da379..84ff3c4 100644 --- a/LocalNotificationBase/AppDelegate.swift +++ b/LocalNotificationBase/AppDelegate.swift @@ -43,6 +43,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func applicationDidBecomeActive(_ application: UIApplication) { + UIApplication.shared.applicationIconBadgeNumber = 0 // 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. } diff --git a/LocalNotificationBase/Base.lproj/Main.storyboard b/LocalNotificationBase/Base.lproj/Main.storyboard index 0d2afb5..d59d81d 100644 --- a/LocalNotificationBase/Base.lproj/Main.storyboard +++ b/LocalNotificationBase/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - + @@ -10,10 +10,10 @@ - + - + @@ -31,9 +31,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LocalNotificationBase/BronzeViewController.swift b/LocalNotificationBase/BronzeViewController.swift new file mode 100644 index 0000000..bfc36b4 --- /dev/null +++ b/LocalNotificationBase/BronzeViewController.swift @@ -0,0 +1,109 @@ +// +// BronzeViewController.swift +// LocalNotificationBase +// +// Created by João Henrique Andrade on 13/06/19. +// Copyright © 2019 Bruno Omella Mainieri. All rights reserved. +// + +import UIKit +import UserNotifications + +class BronzeViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UNUserNotificationCenterDelegate { + + func numberOfComponents(in pickerView: UIPickerView) -> Int { + return 1 + } + func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { + return segundos.count + } + func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { + return String(segundos[row]) + } + + + override func viewDidLoad() { + super.viewDidLoad() + pickerSegundos.delegate = self + pickerSegundos.dataSource = self + UNUserNotificationCenter.current().delegate = self + } + + let segundos:[Int] = [1,2,3,4,5,6,7,8,9] + @IBOutlet weak var switchSom: UISwitch! + @IBOutlet weak var switchBadge: UISwitch! + @IBOutlet weak var pickerSegundos: UIPickerView! + @IBOutlet weak var corpoTexto: UITextField! + @IBOutlet weak var tituloTexto: UITextField! + + @IBAction func enviar(_ nder: Any) { + enviarNotificacao() + } + + func enviarNotificacao(){ + let tituloNotificacao = self.tituloTexto.text + let corpoNotificao = self.corpoTexto.text + let soundOnOff: Bool = switchSom.isOn + let badgeOnOff: Bool = switchBadge.isOn + let valorSegundos : Double = Double(self.segundos[self.pickerSegundos.selectedRow(inComponent: 0)]) + var numeroBadges: Int = 0 + + + let notificationCenter = UNUserNotificationCenter.current() + notificationCenter.getNotificationSettings { (settings) in + if settings.authorizationStatus == .authorized { + + let content = UNMutableNotificationContent() + content.title = NSString.localizedUserNotificationString(forKey: tituloNotificacao!, arguments: nil) + content.body = NSString.localizedUserNotificationString(forKey: corpoNotificao!, arguments: nil) + let userActions = "User Actions" + content.categoryIdentifier = userActions + + if soundOnOff { + content.sound = UNNotificationSound.default + }else { + content.sound = nil + } + + if badgeOnOff{ + numeroBadges += 1 + content.badge = NSNumber(value: numeroBadges) + }else { + content.badge = nil + } + + let trigger = UNTimeIntervalNotificationTrigger(timeInterval: valorSegundos, 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) + } + + let repetirAcao = UNNotificationAction(identifier: "Repetir", title: "Repetir", options: []) + let abrirAplicativo = UNNotificationAction(identifier: "Abrir", title: "Abrir", options: [.foreground]) + let category = UNNotificationCategory(identifier: userActions, actions: [repetirAcao, abrirAplicativo], intentIdentifiers: [], options: []) + notificationCenter.setNotificationCategories([category]) + + } + } else { + print("Impossível mandar notificação - permissão negada") + } + } + + } + + func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { + switch response.actionIdentifier{ + case "Abrir": +// performSegue(withIdentifier: "", sender: nil) + tabBarController?.selectedIndex = 0 + case "Repetir": + enviarNotificacao() + + default: + print("Ocorreu um erro na hora de abrir a notificação") + } + completionHandler() + } +}