Skip to content

[WIP] Add notifications #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions FunnyPuny/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import FirebaseCore
import RealmSwift
import SwiftDate
import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let notificationCenter = UNUserNotificationCenter.current()

func application(
_: UIApplication,
didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
notificationCenter.requestAuthorization(options: options) {
didAllow, error in
if !didAllow {
print("User has declined notifications")
}
}
notificationCenter.delegate = self
FirebaseApp.configure()
SwiftDate.defaultRegion = .local
setupRealm()
Expand All @@ -22,6 +32,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
}

private func setupRealm() {
let config = Realm.Configuration(
schemaVersion: 2)
Realm.Configuration.defaultConfiguration = config
do {
let realm = try Realm()
print("Realm is located at:", realm.configuration.fileURL!)
Expand All @@ -36,3 +49,38 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
window?.makeKeyAndVisible()
}
}

// MARK: Notifications

extension AppDelegate {
func getRequest() {}

func scheduleNotification(notificationType: String) {
let content = UNMutableNotificationContent()
content.title = notificationType
content.body = "This is example how to create " + notificationType
content.sound = UNNotificationSound.default
content.badge = 1

let date = Date(timeIntervalSinceNow: 3600)
let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let identifier = "Local Notification"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

notificationCenter.add(request) { error in
if let error {
print("Error \(error.localizedDescription)")
}
}
}
}

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .sound])
}
}
2 changes: 2 additions & 0 deletions FunnyPuny/Model/Habit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Habit: Object {
@Persisted var name: String = ""
@Persisted var frequency: List<Frequency>
@Persisted var createdDate = Date()
@Persisted var reminderTime: String?
@Persisted var reminderNote: String?

convenience init(name: String, frequency: List<Frequency>, createdDate: Date) {
self.init()
Expand Down
2 changes: 2 additions & 0 deletions FunnyPuny/Presentation/Common /Constants/DateFormats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ extension Date {
case formatLLLLyyyy = "LLLL yyyy"
/// 20221018
case formatyyyyMMdd = "yyyyMMdd"
/// 11:36
case formatHHmm = "HH:mm"
}

var shortForm: String {
Expand Down
14 changes: 13 additions & 1 deletion FunnyPuny/Presentation/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HomeViewController: ViewController {
private var viewState: ViewState = .emptyState

private var homeView = HomeView()

let appDelegate = UIApplication.shared.delegate as? AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
title = Texts.home
Expand Down Expand Up @@ -158,6 +158,18 @@ extension HomeViewController: UITableViewDelegate {
tableView.reloadData()
}
addHapticFeedback(style: .heavy)
let notificationType = "HDHBHDDHBD"
let alert = UIAlertController(title: "",
message: "After 5 seconds " + notificationType + " will appear",
preferredStyle: .alert)

let okAction = UIAlertAction(title: "OK", style: .default) { action in

self.appDelegate?.scheduleNotification(notificationType: notificationType)
}

alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
}

Expand Down