Setting Multiple Times for Notifications in Swift

Setting Multiple Times for Notifications in Swift

import UIKit

class ViewController: UIViewController {

@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var mySwitch7am: UISwitch!
@IBOutlet var mySwitch8am: UISwitch!

var localNotification7am = UILocalNotification()
var localNotification8am = UILocalNotification()
var notificationsCounter = 0

func datePicker() { myDatePicker.datePickerMode = UIDatePickerMode.Date }
func datePickerDefaultDate() { myDatePicker.date = NSDate().hour > 8 ? NSDate().xDays(+1) : NSDate() }

func notificationsOptions7am() {
localNotification7am.timeZone = NSTimeZone.localTimeZone()
localNotification7am.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification7am)
localNotification7am.alertAction = "Open App"
localNotification7am.soundName = UILocalNotificationDefaultSoundName
}
func notificationsOptions8am() {
localNotification7am.timeZone = NSTimeZone.localTimeZone()
localNotification7am.repeatInterval = .CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(localNotification8am)
localNotification7am.alertAction = "Open App"
localNotification7am.soundName = UILocalNotificationDefaultSoundName
}
func toggleSwitch7am(){
localNotification7am.alertBody = "Here is the seven o'clock notification"
localNotification7am.fireDate = mySwitch7am.on ? myDatePicker.date.fireDateAt7am : NSDate().xDays(+36500)
}
func toggleSwitch8am(){
localNotification8am.alertBody = "Here is the eight o'clock notification"
localNotification8am.fireDate = mySwitch8am.on ? myDatePicker.date.fireDateAt8am : NSDate().xDays(+36500)
}
override func viewDidLoad() {
super.viewDidLoad()
datePicker()
datePickerDefaultDate()
notificationsOptions7am()
notificationsOptions8am()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func switchedOn7am(sender: AnyObject) {
toggleSwitch7am()
}
@IBAction func switchedOn8am(sender: AnyObject) {
toggleSwitch8am()
}
}

Extensions:

    import Foundation
extension NSDate {
func xDays(x:Int) -> NSDate {
return NSCalendar.currentCalendar().dateByAddingUnit(.CalendarUnitDay, value: x, toDate: self, options: nil)!
}
var day: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitDay, fromDate: self).day }
var month: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitMonth, fromDate: self).month }
var year: Int { return NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: self).year }
var fireDateAt7am: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 7, minute: 0, second: 0, nanosecond: 0)! }
var fireDateAt8am: NSDate { return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: 8, minute: 0, second: 0, nanosecond: 0)! }
func fireDateAt(hr:Int, min:Int) -> NSDate {
return NSCalendar.currentCalendar().dateWithEra(1, year: year, month: month, day: day, hour: hr, minute: min, second: 0, nanosecond: 0)!
}
}

Run multiple notifications through a function in Swift in Xcode?

It's because the same identifier

 let request = UNNotificationRequest(identifier: "testIdentifier", content: content, trigger: trigger)

so change it for every scheduled notification

How to implement multiple local notifications on specific day of weeks at different times in swift

UILocalNotification is deprecated in iOS 10. Use UNNotificationRequest instead,

import UIKit
import UserNotifications

class ViewController: UIViewController, UNUserNotificationCenterDelegate {

override func viewDidLoad() {
super.viewDidLoad()

//requesting for authorization
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in
if error == nil {
self.addMultipleLocalNotification()
}
})
}

func addMultipleLocalNotification(){
//creating the notification content
let content = UNMutableNotificationContent()

//adding title, subtitle, body and badge
content.title = "Notification title "
content.subtitle = "Notification sub-title "
content.body = "We are learning about iOS Local Notification"
content.badge = 1

var sundayDate = DateComponents()
sundayDate.weekday = 0 // which is Sunday
sundayDate.hour = 10
sundayDate.minute = 00

var wedDate = DateComponents()
wedDate.weekday = 3 // which is wednesday
wedDate.hour = 18
wedDate.minute = 00

let trigger = UNCalendarNotificationTrigger(dateMatching: sundayDate, repeats: true)
let trigger1 = UNCalendarNotificationTrigger(dateMatching: wedDate, repeats: true)
let request = UNNotificationRequest(identifier: "notificationName", content: content, trigger: trigger)
let request2 = UNNotificationRequest(identifier: "notificationName2", content: content, trigger: trigger1)

UNUserNotificationCenter.current().delegate = self

//adding the notification to notification center
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
UNUserNotificationCenter.current().add(request2, withCompletionHandler: nil)

UNUserNotificationCenter.current().getPendingNotificationRequests(
completionHandler: { (notficiations) in
for localNotification in notficiations {
print(localNotification)
}
})
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

//displaying the ios local notification when app is in foreground
completionHandler([.alert, .badge, .sound])
}
}

iOS local notification base on day, time frame and number of times?

If you want to schedule multiple notifications, you need to create multiple notification triggers and register them.

something like:

// Simple extension if you have to create multiple of the same object
extension DateComponents {
static func triggerFor(hour: Int, minute: Int) -> DateComponents {
var component = DateComponents()
component.calendar = Calendar.current
component.hour = hour
component.minute = minute
component.weekday = 1
return component
}
}

// Add each trigger into an array with the correct date component
let triggers = [
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 1, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 2, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 3, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 4, minute: 0), repeats: true),
UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 5, minute: 0), repeats: true)
]

// Iterate through the array and register the notification with the system
for trigger in triggers {
// Create the content of the notification
let content = UNMutableNotificationContent()
content.title = "My Notification Title"
content.body = "Body of Notification"

// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)

// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
}
}
}


Related Topics



Leave a reply



Submit