Local notifications - repeat Interval in swift 3 - ios

I want to repeat local notification every week, before iOS10 there is repeatInterval, but i am not able to find anything suitable to repeat notifications in iOS10.
TimeTrigger and calendarTrigger both have repeat as true or false, where can i apply repeat as weekly, daily, monthly.
Thanks.

Try this.
func scheduleNotification(at date: Date, body: String) {
let triggerWeekly = Calendar.current.dateComponents([.weekday,hour,.minute,.second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Dont Forget"
content.body = body
content.sound = UNNotificationSound.default()
//content.categoryIdentifier = "todoList"
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
//UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}

Related

Cancel a repeating UNNotification for today only

I am scheduling daily repeating notifications by setting only hour, minutes and seconds:
func addNotification(fireDate: Date)
{
let notificationCenter = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Some title"
content.body = "Some message"
content.sound = .default
content.categoryIdentifier = "MyCategory"
let calendar = Calendar(identifier: .gregorian)
let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let requestID = UUID().uuidString
let notificationRequest = UNNotificationRequest(identifier: requestID, content: content, trigger: trigger)
notificationCenter.add(notificationRequest) { error in
if let error = error
{
print(error.localizedDescription)
}
}
}
Now I would like to be able to cancel all notifications for current day, so I could receive notifications starting with tomorrow. I tried to subclass UNCalendarNotificationTrigger in order to change nextTriggerDate, but there is an error when adding a notification using my custom trigger class. Cannot find a way to achieve this, if I cancel a scheduled notification at certain time, I will not receive that notification anymore. Any ideas are appreciated.

How to implement scheduled local notification

I have this code below to test how local notification works every hour. But im not getting anything.
Also, is there a way to send different local notifications messages in different times? and im just calling the LocalNotificationHour() in viewDidLoad()
I just started learning swift, so im sorry in advance.
--
#objc func LocalNotificationHour() {
let user = UNUserNotificationCenter.current()
user.requestAuthorization(options: [.alert,.sound]) { (granted, error) in}
let content = UNMutableNotificationContent()
content.title = "Local Notification"
content.body = "This is a test."
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
user.add(request) { (error) in print("Error")}
}
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
This basically takes todays date and sets the time to 1am. Using: UNCalendarNotificationTrigger(dateMatching: you are telling the notification to trigger at 1am today and then repeat at the same time each day.
To trigger a notification based on a time interval you should use the UNTimeIntervalNotificationTrigger.
// Fire in 60 minutes (60 seconds times 60)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (60*60), repeats: false)
You can schedule your notification for every minute by adding following code:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (success, error) in
if error == nil, !success {
print("Error = \(error!.localizedDescription)")
} else {
let content = UNMutableNotificationContent()
content.title = "Local Notification"
content.body = "This is a test."
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}

Swift iOS 12 Beta UNUserNotification not firing

I'm trying to schedule a local iOS notification, but none pop up. I've verified 100% that the adjustedDate variable is valid, and I've confirmed that the notification trigger has the right date, through its nextTriggerDate function.
I've also verified that UNUserNotificationCenter requests notification permissions in AppDelegate and that works fine.
let content = UNMutableNotificationContent()
content.title = "Test"
content.body = "More testing"
content.sound = UNNotificationSound.default()
let trigger = UNCalendarNotificationTrigger(dateMatching: Calendar.normalizedCalendar.dateComponents([.year, .month, .day, .hour, .minute, .calendar], from: adjustedTime), repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {
error in
if error != nil {
self.out("Failed to add notification: \(error!.localizedDescription)")
} else {
}
}

Local notifications every month except specific date

I have a local notification which repeats every month at a specific day:
func setNotifications(date: Date, forMonth: Bool) {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
if forMonth == true {
let content = UNMutableNotificationContent()
content.title = ""
content.body = "Here is some nice content."
content.sound = UNNotificationSound.default()
let triggerDate = Calendar.current.dateComponents([.day], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let request = UNNotificationRequest(identifier: "MonthNotification", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
}
}
}
}
Let's say the date is 28.01.2018 you will get a notification at 28.02. and so on...
How can I achieve that this notification not appears at another special date (which has this day), for example 28.05.2018?
I hope my question is clear enough.

Local Notification on different dates Swift 3

I want to fire the local notification every year on specific dates but all notifications have different title and body , here is my code that I wrote and thats works perfectly but for one notification only and the second part is after getting notification my app icon have 1, means one notification there how to remove this?..
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.statusBarStyle = .default
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]){ (allowed, error) in
UNUserNotificationCenter.current().delegate = self
scheduleNotification()
return true
}
func scheduleNotification() {
var date = DateComponents()
date.year = 2017
date.month = 6
date.day = 12
date.hour = 22
date.minute = 39
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Schedule Notification"
content.body = "Today is my Birthday"
content.sound = UNNotificationSound.default()
content.badge = 1
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("error: \(error)")
}
}
Appreciate and thanks in advance
For repeat notifications you have to schedule multiple notifications. You can do like this.
func scheduleNotification(_ title:String, body:String, dateComponents :DateComponents) {
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = UNNotificationSound.default()
content.badge = 1
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("error: \(error)")
}
}
}
And use the above function to schedule multiple notifications like this.
var date = DateComponents()
date.year = 2017
date.month = 6
date.day = 12
date.hour = 22
date.minute = 39
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
scheduleNotification("Schedule Notification", body: "Today is my Birthday", dateComponents: date)
scheduleNotification("Schedule Notification 2", body: "Today is my Birthday 2", dateComponents: date2)
scheduleNotification("Schedule Notification 3", body: "Today is my Birthday 3", dateComponents: date3)
Second part of your question how to remove the badge icon. Add following code somewhere in your applicationDidFinishLaunching or applicationDidBecomeActive.
UIApplication.shared.applicationIconBadgeNumber = 0;
Setting a different identifier for every notification gives you the required result.
notificationType - is any String that you can pass while scheduling a notification
let request = UNNotificationRequest(identifier: "Medicine Local Notification\(notificationType)", content: content, trigger: trigger)

Resources