SlideShare a Scribd company logo
1 of 66
Local Notifications
                    Rob Caporetto
                   @rob_caporetto
             www.rob-caporetto.com
What notifications?
What Notifications?
What Notifications?

• Allows you to create 3 types
What Notifications?

• Allows you to create 3 types
 • Badges
What Notifications?

• Allows you to create 3 types
 • Badges
 • Sounds
What Notifications?

• Allows you to create 3 types
 • Badges
 • Sounds
 • Alerts
Lifecycle
Creating a Notification
Creating a Notification

•   User performs an action.
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.

•   Register the notification
    with the UIApplication
    instance.
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.

•   Register the notification
    with the UIApplication
    instance.
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.

•   Register the notification
    with the UIApplication
    instance.
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.

•   Register the notification
    with the UIApplication
    instance.
Receiving a Notification
Receiving a Notification
•   Some time passes…
Receiving a Notification
•   Some time passes…

•   If the device is locked,
    then we receive the
    notification bubble.
Receiving a Notification
•   Some time passes…

•   If the device is locked,
    then we receive the
    notification bubble.

•   If the user acknowledges
    this, the application
    activates and responds
    to the notification.
Receiving a Notification
•   Some time passes…

•   If the device is locked,
    then we receive the
    notification bubble.

•   If the user acknowledges
    this, the application
    activates and responds
    to the notification.
Receiving a Notification
Receiving a Notification

•   If the device is active
    and the application is
    not running, the
    notification bubble will
    appear.
Receiving a Notification

•   If the device is active
    and the application is
    not running, the
    notification bubble will
    appear.

•   If the user responds to
    this, the application will
    activate & respond
Receiving a Notification

•   If the device is active
    and the application is
    not running, the
    notification bubble will
    appear.

•   If the user responds to
    this, the application will
    activate & respond
Receiving a Notification
Receiving a Notification

• If the application is in the foreground,
  then no notification is presented.
Receiving a Notification

• If the application is in the foreground,
  then no notification is presented.
• The application will receive a message on
  the delegate and able to take over.
Adding Local
Notifications
Creating a Notification
Creating a Notification

• Alloc/init a UILocalNotification
  instance.
• Configure its properties.
• Register with the application’s
  UIApplication instance.
Creating a Notification
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
UILocalNotification *notification = [[UILocalNotification alloc] init];

notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.alertAction = @"View";
notification.alertBody = @"Here's our notification message!";
notification.userInfo = userInfo;
Creating a Notification
Creating a Notification

• Scheduling Properties to set:
Creating a Notification

• Scheduling Properties to set:
 • fireDate
Creating a Notification

• Scheduling Properties to set:
 • fireDate
 • timeZone
Creating a Notification

• Scheduling Properties to set:
 • fireDate
 • timeZone
 • repeatInterval
Creating a Notification

• Scheduling Properties to set:
 • fireDate
 • timeZone
 • repeatInterval
 • repeatCalendar
Creating a Notification
Creating a Notification

• Appearance Properties:
Creating a Notification

• Appearance Properties:
 • alertBody
Creating a Notification

• Appearance Properties:
 • alertBody
 • hasAction
Creating a Notification

• Appearance Properties:
 • alertBody
 • hasAction
 • alertAction
Creating a Notification

• Appearance Properties:
 • alertBody
 • hasAction
 • alertAction
 • alertLaunchImage
Creating a Notification
Creating a Notification

• Other properties:
Creating a Notification

• Other properties:
 • applicationIconBadgeNumber
Creating a Notification

• Other properties:
 • applicationIconBadgeNumber
 • soundName
Creating a Notification

• Other properties:
 • applicationIconBadgeNumber
 • soundName
 • userInfo
Scheduling
Scheduling

• Get the UIApplication instance and call
  scheduleLocalNotification:
Scheduling

  • Get the UIApplication instance and call
      scheduleLocalNotification:

[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Accessing Notifications


• Call scheduledLocalNotifications
Cancelling Notifications
Cancelling Notifications

• To cancel a single notification:
 • Call cancelLocalNotification:
Cancelling Notifications

• To cancel a single notification:
 • Call cancelLocalNotification:
• To cancel all notifications:
 • Call cancelAllLocalNotifications
Handling Notifications
Handling Notifications

• Two entry points
Handling Notifications

• Two entry points
 • Application is not running
Handling Notifications

• Two entry points
 • Application is not running
 • Application is in foreground, or
    suspended
Handling Notifications
Handling Notifications


• Not running?
Handling Notifications


• Not running?
 • Check launchOptions dictionary?
Handling Notifications
- (BOOL)application:(UIApplication *)application
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Perform normal delegate tasks...

    UILocalNotification *notification =
      [launchOptions
        objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification != nil)
    {
        // Respond to notification
    }

    return YES;
}
Handling Notifications


• If we’re active, or have been backgrounded?
Handling Notifications
- (void)application:(UIApplication *)application
  didReceiveLocalNotification:(UILocalNotification *)notification
{
    if ([application applicationState] == UIApplicationStateActive)
    {
        // Perform some custom notification logic
    }
}
Other Resources

• “Local and Push Notification Programming
  Guide” - developer.apple.com/iphone
• “Session 129 - Implementing Local and
  Push Notifications” - WWDC 2010 videos

More Related Content

Similar to Local Notifications @ Melbourne Cocoaheads (Aug 10)

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's NewNascentDigital
 
Apple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewApple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewSammy Sunny
 
Getting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingGetting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingBitbar
 
Using multitouch and sensors in Java
Using multitouch and sensors in JavaUsing multitouch and sensors in Java
Using multitouch and sensors in JavaIntel Software Brasil
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkMiguel de Icaza
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter introJintin Lin
 
User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖Maxis Kao
 
Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8Xamarin
 
Uncovering breaking changes behind UI on mobile applications
Uncovering breaking changes behind UI on mobile applicationsUncovering breaking changes behind UI on mobile applications
Uncovering breaking changes behind UI on mobile applicationsKazuaki Matsuo
 
Continuous delivery for native apps
Continuous delivery for native appsContinuous delivery for native apps
Continuous delivery for native appsNiels Frydenholm
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IISivaSankari36
 
iOS: Implementing a Custom View
iOS: Implementing a Custom ViewiOS: Implementing a Custom View
iOS: Implementing a Custom ViewJussi Pohjolainen
 
Getting started with caliburn.micro and windows phone 7
Getting started with caliburn.micro and windows phone 7Getting started with caliburn.micro and windows phone 7
Getting started with caliburn.micro and windows phone 7Gary Park
 
DEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForDEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForMichael Scovetta
 
Cross-platform logging and analytics
Cross-platform logging and analyticsCross-platform logging and analytics
Cross-platform logging and analyticsDrew Crawford
 

Similar to Local Notifications @ Melbourne Cocoaheads (Aug 10) (20)

iOS Development: What's New
iOS Development: What's NewiOS Development: What's New
iOS Development: What's New
 
Apple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical OverviewApple Watch and WatchKit - A Technical Overview
Apple Watch and WatchKit - A Technical Overview
 
Getting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingGetting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App Testing
 
Using multitouch and sensors in Java
Using multitouch and sensors in JavaUsing multitouch and sensors in Java
Using multitouch and sensors in Java
 
watch_kit_v_1.0
watch_kit_v_1.0watch_kit_v_1.0
watch_kit_v_1.0
 
Introduction of Xcode
Introduction of XcodeIntroduction of Xcode
Introduction of Xcode
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections Talk
 
iOS NotificationCenter intro
iOS NotificationCenter introiOS NotificationCenter intro
iOS NotificationCenter intro
 
User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖User first! 前端的最後一塊拼圖
User first! 前端的最後一塊拼圖
 
Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8Xamarin: Introduction to iOS 8
Xamarin: Introduction to iOS 8
 
Uncovering breaking changes behind UI on mobile applications
Uncovering breaking changes behind UI on mobile applicationsUncovering breaking changes behind UI on mobile applications
Uncovering breaking changes behind UI on mobile applications
 
javabeans
javabeansjavabeans
javabeans
 
Iphone course 3
Iphone course 3Iphone course 3
Iphone course 3
 
Continuous delivery for native apps
Continuous delivery for native appsContinuous delivery for native apps
Continuous delivery for native apps
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
iOS: Implementing a Custom View
iOS: Implementing a Custom ViewiOS: Implementing a Custom View
iOS: Implementing a Custom View
 
Getting started with caliburn.micro and windows phone 7
Getting started with caliburn.micro and windows phone 7Getting started with caliburn.micro and windows phone 7
Getting started with caliburn.micro and windows phone 7
 
DEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking ForDEFCON 18- These Aren't the Permissions You're Looking For
DEFCON 18- These Aren't the Permissions You're Looking For
 
Cross-platform logging and analytics
Cross-platform logging and analyticsCross-platform logging and analytics
Cross-platform logging and analytics
 

Local Notifications @ Melbourne Cocoaheads (Aug 10)

Editor's Notes

  1. - OS3 introduced push notifications - allowing an external server to push to your phone. - Required you to maintain external infrastructure (and was complicated!) - Local notifications allow you to generate notifications without relying upon the external infrastructure - One of Steve’s tentpole’s with iOS 4
  2. - Badges: Increment/Decrement the icon’s badge (on the Springboard) - Sounds: Play an alert tone when the notification is fired - Alerts: Display a message on screen
  3. - Badges: Increment/Decrement the icon’s badge (on the Springboard) - Sounds: Play an alert tone when the notification is fired - Alerts: Display a message on screen
  4. - Badges: Increment/Decrement the icon’s badge (on the Springboard) - Sounds: Play an alert tone when the notification is fired - Alerts: Display a message on screen
  5. - Badges: Increment/Decrement the icon’s badge (on the Springboard) - Sounds: Play an alert tone when the notification is fired - Alerts: Display a message on screen
  6. - the basic notification lifecycle for the user (in case no one has seen these in action)
  7. - Create the notification - Set the details - Register with the UIApplication instance.
  8. - Create the notification - Set the details - Register with the UIApplication instance.
  9. - Create the notification - Set the details - Register with the UIApplication instance.
  10. - Create the notification - Set the details - Register with the UIApplication instance.
  11. - Create the notification - Set the details - Register with the UIApplication instance.
  12. - Create the notification - Set the details - Register with the UIApplication instance.
  13. - Notification will appear on the lock screen (with the Slide to View) text. -
  14. - Notification will appear on the lock screen (with the Slide to View) text. -
  15. - Notification will appear on the lock screen (with the Slide to View) text. -
  16. - Notification will appear on the lock screen (with the Slide to View) text. -
  17. - That is, you’re looking at the application, right as the notification is fired!
  18. - That is, you’re looking at the application, right as the notification is fired!
  19. - EB Sample code (with modifications for clarity). - We create a dictionary for User Info (we populate this in the actual code, but leaving it empty here). -
  20. - fireDate: NSDate defining the scheduled time. If you want a repeating notification, this is the time of the first scheduled notification. - timeZone: Defines the timeZone the notification should be scheduled in. Can be left at nil - treats the date as a GMT time. Otherwise, treats will update the time based on the timezone. - repeatInterval: Defines the calendar period (NSCalendarUnit) to repeat the notification (ie. every day/week/month/year) - repeatCalendar: Defines the NSCalendar used to schedule the repeat notifications.
  21. - fireDate: NSDate defining the scheduled time. If you want a repeating notification, this is the time of the first scheduled notification. - timeZone: Defines the timeZone the notification should be scheduled in. Can be left at nil - treats the date as a GMT time. Otherwise, treats will update the time based on the timezone. - repeatInterval: Defines the calendar period (NSCalendarUnit) to repeat the notification (ie. every day/week/month/year) - repeatCalendar: Defines the NSCalendar used to schedule the repeat notifications.
  22. - fireDate: NSDate defining the scheduled time. If you want a repeating notification, this is the time of the first scheduled notification. - timeZone: Defines the timeZone the notification should be scheduled in. Can be left at nil - treats the date as a GMT time. Otherwise, treats will update the time based on the timezone. - repeatInterval: Defines the calendar period (NSCalendarUnit) to repeat the notification (ie. every day/week/month/year) - repeatCalendar: Defines the NSCalendar used to schedule the repeat notifications.
  23. - fireDate: NSDate defining the scheduled time. If you want a repeating notification, this is the time of the first scheduled notification. - timeZone: Defines the timeZone the notification should be scheduled in. Can be left at nil - treats the date as a GMT time. Otherwise, treats will update the time based on the timezone. - repeatInterval: Defines the calendar period (NSCalendarUnit) to repeat the notification (ie. every day/week/month/year) - repeatCalendar: Defines the NSCalendar used to schedule the repeat notifications.
  24. - fireDate: NSDate defining the scheduled time. If you want a repeating notification, this is the time of the first scheduled notification. - timeZone: Defines the timeZone the notification should be scheduled in. Can be left at nil - treats the date as a GMT time. Otherwise, treats will update the time based on the timezone. - repeatInterval: Defines the calendar period (NSCalendarUnit) to repeat the notification (ie. every day/week/month/year) - repeatCalendar: Defines the NSCalendar used to schedule the repeat notifications.
  25. - alertBody: Defines the message which will appear in the alert bubble. - hasAction: Controls whether the action buttons (or slider) are shown or hidden. If you set to NO, requires alertBody to have a value! - alertAction: Title of the Slider/Action button on the pop-up - alertLaunchImage: A filename in the app bundle to an image which is to be used instead of Default.png when the app is launched.
  26. - alertBody: Defines the message which will appear in the alert bubble. - hasAction: Controls whether the action buttons (or slider) are shown or hidden. If you set to NO, requires alertBody to have a value! - alertAction: Title of the Slider/Action button on the pop-up - alertLaunchImage: A filename in the app bundle to an image which is to be used instead of Default.png when the app is launched.
  27. - alertBody: Defines the message which will appear in the alert bubble. - hasAction: Controls whether the action buttons (or slider) are shown or hidden. If you set to NO, requires alertBody to have a value! - alertAction: Title of the Slider/Action button on the pop-up - alertLaunchImage: A filename in the app bundle to an image which is to be used instead of Default.png when the app is launched.
  28. - alertBody: Defines the message which will appear in the alert bubble. - hasAction: Controls whether the action buttons (or slider) are shown or hidden. If you set to NO, requires alertBody to have a value! - alertAction: Title of the Slider/Action button on the pop-up - alertLaunchImage: A filename in the app bundle to an image which is to be used instead of Default.png when the app is launched.
  29. - alertBody: Defines the message which will appear in the alert bubble. - hasAction: Controls whether the action buttons (or slider) are shown or hidden. If you set to NO, requires alertBody to have a value! - alertAction: Title of the Slider/Action button on the pop-up - alertLaunchImage: A filename in the app bundle to an image which is to be used instead of Default.png when the app is launched.
  30. applicationIconBadgeNumber: what count appears on the application’s badge soundName: Filename to a sound file in the application bundle (needs to be <30 seconds, using AIFF/WAV/CAF formats. See Apple’s docs for more info). userInfo: Dictionary containing application specific details. Must be valid property list types.
  31. applicationIconBadgeNumber: what count appears on the application’s badge soundName: Filename to a sound file in the application bundle (needs to be <30 seconds, using AIFF/WAV/CAF formats. See Apple’s docs for more info). userInfo: Dictionary containing application specific details. Must be valid property list types.
  32. applicationIconBadgeNumber: what count appears on the application’s badge soundName: Filename to a sound file in the application bundle (needs to be <30 seconds, using AIFF/WAV/CAF formats. See Apple’s docs for more info). userInfo: Dictionary containing application specific details. Must be valid property list types.
  33. applicationIconBadgeNumber: what count appears on the application’s badge soundName: Filename to a sound file in the application bundle (needs to be <30 seconds, using AIFF/WAV/CAF formats. See Apple’s docs for more info). userInfo: Dictionary containing application specific details. Must be valid property list types.
  34. - Returns an Array of the pending notifications for your application (those which haven’t yet fired). - Any notifications which you submitted & have been fired are lost - that may mean you need to cache notifications independently. - UILocalNotification objects are NSCoding compliant, so they can be serialised out.
  35. - Call cancelLocalNotification: with the UILocalNotification instance you wish to remove.
  36. - Call cancelLocalNotification: with the UILocalNotification instance you wish to remove.
  37. Not running? We then enter the application:didFinishLaunchingWithOptions: method in our app delegate. Check the launchOptions dictionary for the presence of UIApplicationLaunchOptionsLocalNotificationKey. If present, the associated value is a UILocalNotification object, which we can then process.
  38. Not running? We then enter the application:didFinishLaunchingWithOptions: method in our app delegate. Check the launchOptions dictionary for the presence of UIApplicationLaunchOptionsLocalNotificationKey. If present, the associated value is a UILocalNotification object, which we can then process.
  39. - is the replacement for applicationDidFinishLaunching: delegate method (added in iOS 3)
  40. - Delegate’s application:didReceiveLocalNotification: method with the UILocalNotification instance
  41. - calling [application applicationState] lets us know if we’re Active (meaning in foreground), Background (was running in background), or Inactive. - I used this check so I could emulate the notification alertview (as I didn’t want my app to automatically respond). - Depending on your own app, you may want it to respond automatically.