From 950435d946052aa4acfb55bb29a702ed2d4079b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Henrique=20Andrade?= Date: Thu, 13 Jun 2019 17:57:44 -0300 Subject: [PATCH] =?UTF-8?q?Jo=C3=A3o=20Prata=20e=20Bronze?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 6148 bytes .../project.pbxproj | 4 + LocalNotificationBase/AppDelegate.swift | 1 + .../Base.lproj/Main.storyboard | 122 +++++++++++++++++- .../BronzeViewController.swift | 109 ++++++++++++++++ 5 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 LocalNotificationBase/BronzeViewController.swift diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 - + @@ -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() + } +}