iOS Swift Local notification not "popping up" - ios

I'm trying to send a local notification at scheduled time. But the notifications does not appear on the screen, but it is showing in the notification center when I swipe down.
This is what I'm trying to achieve This is what I getting.
This code is from my AppDelegate's didFinishLaunchingWithOptions().
// Setup local push notifications
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil))
scheduleNotifications()
And this is the code for scheduleNotifications()
func scheduleNotifications() {
// create a corresponding local notification
let notification = UILocalNotification()
// Get today's date, time and year
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year], fromDate: NSDate())
// Sets the fire time to 2pm/1400 hours to anticipate user for lunch time
components.hour = 19
components.minute = 13
components.second = 00
notification.fireDate = components.date // Sets the fire date
notification.alertBody = "Enjoyed your lunch? Don't forget to track your expenses!"
notification.alertAction = "Add expense"
notification.repeatInterval = NSCalendarUnit.Day // Repeats the notifications daily
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Any help would be appreciated. Thanks!

Your issue is the way you are converting the NSDateComponents object to an NSDate.
Simply calling components.date without setting a calendar for the NSDateComponents object will return nil.
Try using this instead:
notification.fireDate = calendar.dateFromComponents(components)
Alternatively, you can set the calendar property on the components object:
components.calendar = calendar
Because you are setting the fireDate property on the notification object to nil, it will fire immediately, i.e. before you have a chance to close the app and lock the screen. This behavour is documented in the UILocalNotification class reference

To me, when phone is connected with a cable, then notification shows up only in notification center - banner is not displayed when app is in foreground.
When cable is disconnected, then the banner is presented over the app.

I get the same odd behavior. Just created a new project to test your code.
What i noticed when you change your fireDate to:
notification.fireDate = NSDate(timeIntervalSinceNow: 10)
You will get your wanted behavior.
Hope that helps a little!

Check Settings > YourApp > Notifications.
Verify the permissions there. "Do Not Disturb" should also not be active.

Related

Local Notifications delivering multiple times instead of just one

I am writing an application and want to include local notifications. I want the notification to deliver at a set time and repeat everyday.
I programmed this into my application, and it does notify the user. However, instead of the user just being notified one time everyday at the set time, the application sends the user 13+ notifications at that set time. The number of notifications is only increasing every day.
How do I fix this issue? I just want the notification to notify the user only once everyday.
Here is my code that I have been using. It is set in the Did Finish Launching with Options of the App Delegate.
let notificationSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
let title: String = "Read Today's Pulse Habit!"
let calendar = NSCalendar.currentCalendar()
let calendarComponents = NSDateComponents()
calendarComponents.hour = 15
calendarComponents.minute = 30
calendarComponents.second = 0
calendar.timeZone = NSTimeZone.defaultTimeZone()
let dateToFire = calendar.dateFromComponents(calendarComponents)
let notification = UILocalNotification()
notification.alertBody = "\(title)"
notification.alertAction = "Read Now"
notification.fireDate = dateToFire
notification.alertTitle = "PULSE"
notification.repeatInterval = NSCalendarUnit.Day
notification.soundName = UILocalNotificationDefaultSoundName
notification.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduleLocalNotification(notification)
I've even tried taking out the line that includes the incrementing icon badge number and that did not make any difference.

Local notification not firing if repeat is set to daily

I've been facing a very strange error when trying to schedule a notification. The user provides the hour and minute when they want to have their local notification, and they can choose if they want it to be scheduled daily weekly or monthly.
However, I have run into a very weird problem. This is my code:
func enableLocalNotifications() {
notificationsSwitch.isOn = true
notificationsOn = true
//first cancel all current local notifications
UIApplication.shared.cancelAllLocalNotifications()
//create new one according to input
let calendar = Calendar(identifier: .gregorian)
var fireComponents=calendar.dateComponents([.day, .month, .year], from:NSDate() as Date)
fireComponents.hour = notificationHour
fireComponents.minute = notificationMinute
fireComponents.second = 00
fireComponents.calendar = calendar
let notification = UILocalNotification()
notification.timeZone = NSTimeZone.local
notification.alertBody = notificationDescription
notification.alertTitle = "Reminder"
notification.fireDate = fireComponents.date
notification.soundName = UILocalNotificationDefaultSoundName
switch notificationRepeat {
case "Daily":
notification.repeatInterval = NSCalendar.Unit.minute
case "Weekly":
notification.repeatInterval = NSCalendar.Unit.weekOfYear
case "Monthly":
notification.repeatInterval = NSCalendar.Unit.month
default:
break
}
UserDefaults.standard.setValue(notificationRepeat, forKey: "notificationRepeat")
UserDefaults.standard.setValue(notificationDescription, forKey: "notificationDescription")
UserDefaults.standard.setValue(notificationHour, forKey: "notificationHour")
UserDefaults.standard.setValue(notificationMinute, forKey: "notificationMinute")
UserDefaults.standard.setValue(true, forKey: "notificationSet")
}
}
The funny thing is, if I for example want to create a "daily" notification at 12:00, and at the moment it is 11:59, it just won't fire the notification at 12:00, actually it fires nothing at all. However, I have noticed, if I change the repeatInterval to NSCalendar.Unit.minute, then suddenly, if I schedule a notification for 12:00, and it's 11:59, I really receive it at 12:00.
Is there any fix for my bug?
Double check whether the date you are setting to notification.fireDate is correct or not. Sometimes due to not setting proper timezones the date and time changes and it won't come at your expecting time.

Local notifications firing immediately instead of when they are scheduled

I've been trying to schedule local notifications by weekday, but they seem to be firing immediately instead of when I schedule them. This is my code in AppDelegate in didFinishLaunchingWithOptions:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert, categories: nil))
var notification = UILocalNotification()
var components = NSDateComponents()
var calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
components.weekday = 5
components.hour = 9
components.minute = 24
notification.alertBody = "Notification test"
notification.fireDate = calendar?.dateFromComponents(components)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Any clues on why it might not be working?
As others have mentioned, make sure to specify the year and month properties of the components object.
components.year = 2016
components.month = 6
Part of the current fire date of your code is the following:
0001-01-01
Which means the notification will be fired at year "1" in January, not in 2016.
You need to use notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
Try to set a year/month because it is created from 01/01/1970, so the date is in the past => immediate trigger

Swift handle date change event

How can I handle date change event in swift?
I want to create a set of notification when the date changes.
var localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = " Woww it works!!"
localNotification.fireDate = date
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
You can subscribe to UIApplicationSignificantTimeChangeNotification. This one should fire when the day changes.
Unfortunately this one also fires on other occasions. So you should store a static variable with the date the last time this notification fired and then check if it actually changed the day.
I don't know what you're gonna use it for, but in most cases the general significant time change is what one is looking for.
You could do localNotification.fireDate = NSDate(timeIntervalSinceNow: 1) which will give you a notification 1 day from the current date. This could be further customized to your liking depending on how specific you want the timing to be.
EDIT: As was pointed out in the comments, the timeIntervalSinceNow is calculated in seconds, so 1 would be one second, not one day.
EDIT 2: Trying to answer the question that was asked. If you want to create a notification at midnight every day...
First, create an NSCalendar object
var calendar = NSCalendar()
var calendarComponents = NSDateComponents()
calendarComponents.setDay(29)
calendarComponents.setMonth(6)
calendarComponents.setYear(2015)
calendarComponents.setHour(12)
calendarComponents.setSeconds(0)
calendarcomponents.setMinutes(0)
calendar.setTimeZone(NSTimeZone.defaultTimeZone)
var dateToFire = calendar.dateFromComponents(calendarComponents)
Now we can schedule the notification daily.
localNotification.fireDate = dateToFire
localNotification.setTimeZone(NSTimeZone.defaultTimeZone)
localNotification.setRepeatInterval(kcfCalendarUnitDay)
Syntax might not be perfect, I was translating from Obj-C, but you should get the general idea.
Try adding observer for NSNotification.Name.NSCalendarDayChanged
The down side of this that it triggers only when app is in foreground

UILocalNotification repeatInterval keeps pushing notifications

When using repeatInteval the notifications keep being pushed one after the other regardless if set on Minutes/Day/Hour etc.
It did seem to work fine until I tested every few seconds now settings won't change back. Any reason why ?
var dateComp:NSDateComponents = NSDateComponents()
dateComp.year = 2015;
dateComp.month = 06;
dateComp.day = 03;
dateComp.hour = 15;
dateComp.minute = 27;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
var notification:UILocalNotification = UILocalNotification()
notification.category = "Daily"
notification.alertBody = "OK"
notification.fireDate = date
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(notification)
If you set the notification to be repeated with CalendarUnitDay it should repeat each day at the same time after the first fire.
Pay attention that to delete scheduled notification is not sufficient to delete the app (at least it was in iOS7) because the system keeps the notification registered but silent for 24h to avoid accidental uninstall.
Maybe you are still seeing old scheduled notifications.
To be sure put a breakpoint and ask the app delegate for its -scheduledNotifications if you find more than you expected this is the source of your problems.
If you tested with every few seconds just once means that all notifications are scheduled and will be received. Try cancel all the scheduled notifications first and then reschedule at desired time interval
UIApplication.sharedApplication().cancelAllLocalNotifications()

Resources