Get Notification and List Notification Swift 4



This time, I’ve created a simple app that demonstrates the use of NSNotifcationCenter so that we can better understand what this would look like in code, and how it actually works. If you’re like me and you’re a hands on type of learner — feel free to code along.

1. Layout in xib file

//original Scren Shot


2. Full Code swift file

import UIKit import UserNotifications class NotificationViewController: UIViewController { @IBOutlet weak var tabelView: UITableView! let koo = "You Get Notification Coy" var users = [String]() override func viewDidLoad() { super.viewDidLoad() tabelView.delegate = self tabelView.dataSource = self tabelView.register(UINib(nibName: "ApiTableViewCell", bundle: nil), forCellReuseIdentifier: "cell") tabelView.tableFooterView = UIView(frame: CGRect.zero) UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (succes, error) in if error != nil { print("Unsusses") } else { print("succesful") } } } @IBAction func bNotif(_ sender: UIButton) { self.users.append(self.koo) tabelView.beginUpdates() let indexPath = IndexPath(row: users.count - 1, section: 0) tabelView.insertRows(at: [indexPath], with: .automatic) tabelView.endUpdates() view.endEditing(true) timedNotification(inSecond: 1) { (success) in if success { print("it's success notif") } } } func timedNotification(inSecond: TimeInterval, complition: @escaping (_ Success: Bool) -> ()) { let trigger = UNTimeIntervalNotificationTrigger(timeInterval: inSecond, repeats: false) let content = UNMutableNotificationContent() content.title = "You Have Notification" content.subtitle = "Ok you succes learn this in the weekend" content.body = "no matter what you are just change your self ok ok ok " let request = UNNotificationRequest(identifier: "cNotif", content: content, trigger: trigger) UNUserNotificationCenter.current().add(request) { (error) in if error != nil { complition(false) } else { complition(true) } } } } extension NotificationViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return users.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ApiTableViewCell cell.merkLabel.text = users[indexPath.row] return cell } //for edit / delete func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { users.remove(at: indexPath.row) tableView.beginUpdates() tableView.deleteRows(at: [indexPath], with: .automatic) tableView.endUpdates() } } }






No comments: