UILocalNotification not doing anything - ios

This may seem like a silly question, but this is my first time using UILocalNotification and I can't get it to work for a quick test. It just doesn't do anything.
1. I've created 2 variables in the AppDelegate
let today = NSDate()
let notification: UILocalNotification = UILocalNotification()
2. Then in the applicationDidEnterBackground function, I have the following
notification.fireDate = today.dateByAddingTimeInterval(10)
notification.alertTitle = "My App Test"
notification.alertBody = "Testing Notification \n :)"
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
3. Also added this to the applicationDidBecomeActive function
UIApplication.sharedApplication().cancelAllLocalNotifications()

After reading the documentation again, I realized I missed a crucial first step which is to register my App first for user notifications. The Apple doc was written in OBJ-C, but I was able to figure it out in order to convert it to swift. This is what I did:
1. I added this to my AppDelegate didFinishLaunchingWithOptions function and it now works
var types: UIUserNotificationType = UIUserNotificationType()
types.insert(UIUserNotificationType.Alert)
types.insert(UIUserNotificationType.Badge)
let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

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.

Apple watch notification mirroring

I'm trying to create a notification with an action on the Apple Watch. However the documentation is not really saying what is native and what should be made.
Currently I'm creating a UILocalNotification on the iPhone. However what I'm wondering is that the action button will be mirrored on the Apple Watch.
The code I'm using to create the notification on the iPhone is:
let incrementAction = UIMutableUserNotificationAction()
incrementAction.identifier = "OPEN_ACTION"
incrementAction.title = "Open"
incrementAction.activationMode = UIUserNotificationActivationMode.Background
incrementAction.authenticationRequired = false
incrementAction.destructive = false
let counterCategory = UIMutableUserNotificationCategory()
counterCategory.identifier = "SLAGBOOM_CATEGORY"
counterCategory.setActions([incrementAction],
forContext: UIUserNotificationActionContext.Default)
counterCategory.setActions([incrementAction],
forContext: UIUserNotificationActionContext.Minimal)
let types = UIUserNotificationType.Alert
let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
let notification:UILocalNotification = UILocalNotification()
notification.alertBody = message
notification.category = "SLAGBOOM_CATEGORY"
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Am I supposed to do anything to make sure the button will work on the Apple Watch? Or do I need to create a watchkit app only for a notification?
You get default notifications for free but the users only option is to dismiss it.
If you want something custom then you want to create your own watch target and build out the different notification views in the watch targets storyboard
Here's the page on Apple's Watch Programming Kit the explains the different custom notifications you can make

iOS Swift Local notification not "popping up"

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.

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()

EKReminder: Reminder with alarm not appearing in NotificationCenter

I tried to add an alarm (EKAlarm) to a reminder (EKReminder). Almost everything works fine: The reminder is added in the "Reminders.app" and gets fired at the right time.
But somehow (in contrast to a manually created reminder) the firing date/time is not showing in the detail label of the reminder in the "Reminders.app" and it's not appearing in the NotificationCenter as upcoming reminder either.
This is my (slightly abbreviated) code:
// EventStore
var store = EKEventStore()
// Reminder
var reminder = EKReminder(eventStore: store)
reminder.title = "Test"
reminder.calendar = store.defaultCalendarForNewReminders()
// Add alarm to reminder
reminder.addAlarm(EKAlarm(absoluteDate: datePicker.date))
// Add it to Reminders.app
store.saveReminder(reminder, commit: true, error: &error)
Am I missing anything? Thx for your help in advance.

Resources