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)
Related
In my app, a user will set date and time for reminder, and the phone needs to send local notification at that particular time even when the app is closed.
Able to send a notification at particular time using the below method. but it doesn't work when app is closed:
public func simpleAddNotification(hour: Int, minute: Int, identifier: String, title: String, body: String) {
// Initialize User Notification Center Object
let center = UNUserNotificationCenter.current()
// The content of the Notification
let content = UNMutableNotificationContent()
content.title = title
content.body = body
content.sound = .default
content.badge = 1
// The selected time to notify the user
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = hour
dateComponents.minute = minute
// The time/repeat trigger
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Initializing the Notification Request object to add to the Notification Center
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
// Adding the notification to the center
center.add(request) { (error) in
if (error) != nil {
print(error!.localizedDescription)
}
}
}
I have this app with a daily notification at 12pm. If the user does something, I want to cancel the notification that day.
Is there a way to cancel the notification that day, but the rest of the days keep working the same?
private func setUpNotification() {
// Ask Permission
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
}
// Create content for notification
let content = UNMutableNotificationContent()
content.title = "I'm a notification!"
content.body = "Don't forget to track your cigarretes!"
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.hour = 12
dateComponents.minute = 00
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
//Register the request
center.add(request) { (error) in
if error != nil {
// Handle any errors.
print(error?.localizedDescription)
}
}
}
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)
}
}
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.
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)")
}
}
}