Created
August 29, 2023 10:28
-
-
Save MehmetUzel/ec89ee8590b0277dd3850a529c4ec5db to your computer and use it in GitHub Desktop.
Swift Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AppDelegate.swift | |
class AppDelegate: NSObject, UIApplicationDelegate { | |
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { | |
UNUserNotificationCenter.current().delegate = self | |
return true | |
} | |
} | |
func get_notification_status(completion: @escaping (Bool) -> Void) { | |
UNUserNotificationCenter.current().getNotificationSettings { settings in | |
DispatchQueue.main.async { | |
var status = false | |
if settings.authorizationStatus == .notDetermined { | |
print("Notification permission is yet to be asked. Go for it!") | |
} else if settings.authorizationStatus == .denied { | |
print("Notification permission was denied previously. Go to settings & privacy to re-enable the permission.") | |
} else if settings.authorizationStatus == .authorized { | |
print("Notification permission already granted.") | |
status = true | |
} | |
completion(status) | |
} | |
} | |
} | |
func removeNotifications() { | |
let center = UNUserNotificationCenter.current() | |
center.getPendingNotificationRequests{ notificationRequests in | |
for notificationRequest:UNNotificationRequest in notificationRequests { | |
center.removePendingNotificationRequests(withIdentifiers: [notificationRequest.identifier]) | |
} | |
} | |
} | |
func scheduleNotification(day: Int) { | |
let center = UNUserNotificationCenter.current() | |
let content = UNMutableNotificationContent() | |
content.title = NSLocalizedString("Ödemelerinizi Yaptınız Mı", comment: "") | |
content.body = NSLocalizedString("Bu ayın ödemelerini yaptıysanız uygulamayı ziyaret ederek hızlıca kaydedebilirsiniz", comment: "") | |
content.sound = UNNotificationSound.default | |
var dateComp = DateComponents() | |
dateComp.day = day; | |
dateComp.hour = 14; | |
dateComp.minute = 00; | |
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComp, repeats: true) | |
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) | |
center.add(request) { error in | |
if let error = error { | |
print("Error scheduling notification: \(error.localizedDescription)") | |
} | |
} | |
} | |
extension AppDelegate: UNUserNotificationCenterDelegate { | |
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { | |
// Here we actually handle the notification | |
print("Notification received with identifier \(notification.request.identifier)") | |
// So we call the completionHandler telling that the notification should display a banner and play the notification sound - this will happen while the app is in foreground | |
completionHandler([.banner, .sound]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment