Reputation: 39
I am implementing two local notifications with different timings. These two notifications scheduling weekly bases and every day it will repeat after 1 minute untill user take action, but the problem is that both notifications showing together but i also have different timing for both, I dont know where is the problem is.. Here is my Code
import UIKit
import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
var isGrantedAccess = false
var weekdaySet = [2,3,4,5,6,7]
@IBOutlet weak var checkinDateLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
checkinNotif()
checkoutNotif()
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedAccess = granted
if granted{
self.setCategories()
} else {
let alert = UIAlertController(title: "Notification Access", message: "In order to use this application, turn on notification permissions.", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "Okay", style: .default, handler: nil)
alert.addAction(alertAction)
self.present(alert , animated: true, completion: nil)
}
})
}
//MARK: - Functions
func setCategories(){
let markAttendeanceAction = UNNotificationAction(identifier: "markAttendeance", title: "Mark Attendence", options: [UNNotificationActionOptions.foreground])
let ignoreAction = UNNotificationAction (identifier: "ignore", title: "Ignore", options: [])
let alarmCategory1 = UNNotificationCategory(identifier: "alarm.category1",actions: [markAttendeanceAction, ignoreAction],intentIdentifiers: [], options: [])
let alarmCategory2 = UNNotificationCategory(identifier: "alarm.category2",actions: [markAttendeanceAction, ignoreAction],intentIdentifiers: [], options: [])
let alarmCategory3 = UNNotificationCategory(identifier: "alarm.category3",actions: [markAttendeanceAction, ignoreAction],intentIdentifiers: [], options: [])
let alarmCategory4 = UNNotificationCategory(identifier: "alarm.category4",actions: [markAttendeanceAction, ignoreAction],intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([alarmCategory1, alarmCategory2, alarmCategory3,alarmCategory4])
}
func checkinNotif(){
let content = UNMutableNotificationContent()
content.title = "CheckIn Alert"
content.body = "Please Check in"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "alarm.category1"
for weekday in weekdaySet {
var datee = DateComponents()
datee.weekday = weekday
datee.hour = 12
datee.minute = 34
datee.timeZone = .current
let triggerDate1 = UNCalendarNotificationTrigger(dateMatching: datee, repeats: true)
let checkinNotification = UNNotificationRequest(identifier: "checkInRepeat", content: content, trigger: triggerDate1)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(checkinNotification, withCompletionHandler: nil)
}
let trigger1 = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let checkinReapeatNotification = UNNotificationRequest(identifier: "checkInRepeat", content: content, trigger: trigger1)
UNUserNotificationCenter.current().add(checkinReapeatNotification, withCompletionHandler: nil)
}
func checkoutNotif(){
let content1 = UNMutableNotificationContent()
content1.title = "Checkout Alert"
content1.body = "Please Checkout"
content1.sound = UNNotificationSound.default()
content1.categoryIdentifier = "alarm.category2"
for weekday in weekdaySet {
var datee = DateComponents()
datee.weekday = weekday
datee.hour = 12
datee.minute = 37
datee.timeZone = .current
let triggerDate1 = UNCalendarNotificationTrigger(dateMatching: datee, repeats: true)
let checkOutNotification = UNNotificationRequest(identifier: "checkOutRepeat", content: content1, trigger: triggerDate1)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(checkOutNotification, withCompletionHandler: nil)
}
let trigger1 = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let checkoutReapeatNotification = UNNotificationRequest(identifier: "checkOutRepeat", content: content1, trigger: trigger1)
UNUserNotificationCenter.current().add(checkoutReapeatNotification, withCompletionHandler: nil)
}
//MARK: - Actions
// MARK: - Delegates
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.content.categoryIdentifier == "alarm.category1" {
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
var identifiers: [String] = []
for notification:UNNotificationRequest in notificationRequests {
if notification.identifier == "checkInRepeat" {
identifiers.append(notification.identifier)
}
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
}else if response.notification.request.content.categoryIdentifier == "alarm.category2" {
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
var identifiers: [String] = []
for notification:UNNotificationRequest in notificationRequests {
if notification.identifier == "checkOutRepeat" {
identifiers.append(notification.identifier)
}
}
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
}
}
completionHandler()
}
}
Upvotes: 1
Views: 49
Reputation: 18591
When you add a UNNotificationRequest
with the same identifier as a pre-existing one, you update it, it will not create a new one.
In your code checkinNotification
and checkinReapeatNotification
have the same identifier. So the only notification that will show up is checkinReapeatNotification
since it is the last one added to the notification center. Same goes for checkOutNotification
and checkoutReapeatNotification
.
And since you are calling checkinNotif()
and checkoutNotif()
consecutively, both checkinReapeatNotification
and checkoutReapeatNotification
, will be fired after 60 seconds.
Use withCompletionHandler
to your advantage: when checkinNotification
is created, then create checkinReapeatNotification
and add a new notification with a new identifier. Do the same for checkOutNotification
.
Upvotes: 2