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.

•  ...
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.

•  ...
Creating a Notification

•   User performs an action.

•   Create a
    UILocalNotification
    instance & configure it.

•  ...
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...
Receiving a Notification
•   Some time passes…

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

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

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

•   If the device is active
    and the application is
    not running, the
    notification bubbl...
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 ...
Adding Local
Notifications
Creating a Notification
Creating a Notification

• Alloc/init a UILocalNotification
  instance.
• Configure its properties.
• Register with the appli...
Creating a Notification
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
UILocalNotification *notificati...
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...
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:
 ...
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...
Handling Notifications


• If we’re active, or have been backgrounded?
Handling Notifications
- (void)application:(UIApplication *)application
  didReceiveLocalNotification:(UILocalNotification ...
Other Resources

• “Local and Push Notification Programming
  Guide” - developer.apple.com/iphone
• “Session 129 - Implemen...
Upcoming SlideShare
Loading in …5
×

Local Notifications @ Melbourne Cocoaheads (Aug 10)

3,727 views
3,534 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,727
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
44
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

  • - 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
  • - 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
  • - 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
  • - 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
  • - 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
  • - the basic notification lifecycle for the user (in case no one has seen these in action)

  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Create the notification
    - Set the details
    - Register with the UIApplication instance.
  • - Notification will appear on the lock screen (with the Slide to View) text.
    -
  • - Notification will appear on the lock screen (with the Slide to View) text.
    -
  • - Notification will appear on the lock screen (with the Slide to View) text.
    -
  • - Notification will appear on the lock screen (with the Slide to View) text.
    -






  • - That is, you’re looking at the application, right as the notification is fired!
  • - That is, you’re looking at the application, right as the notification is fired!


  • - 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).
    -
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • - 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.
  • 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.
  • 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.
  • 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.
  • 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.


  • - 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.
  • - Call cancelLocalNotification: with the UILocalNotification instance you wish to remove.

  • - Call cancelLocalNotification: with the UILocalNotification instance you wish to remove.




  • 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.
  • 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.
  • - is the replacement for applicationDidFinishLaunching: delegate method (added in iOS 3)

  • - Delegate’s application:didReceiveLocalNotification: method with the UILocalNotification instance
  • - 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.

  • Local Notifications @ Melbourne Cocoaheads (Aug 10)

    1. 1. Local Notifications Rob Caporetto @rob_caporetto www.rob-caporetto.com
    2. 2. What notifications?
    3. 3. What Notifications?
    4. 4. What Notifications? • Allows you to create 3 types
    5. 5. What Notifications? • Allows you to create 3 types • Badges
    6. 6. What Notifications? • Allows you to create 3 types • Badges • Sounds
    7. 7. What Notifications? • Allows you to create 3 types • Badges • Sounds • Alerts
    8. 8. Lifecycle
    9. 9. Creating a Notification
    10. 10. Creating a Notification • User performs an action.
    11. 11. Creating a Notification • User performs an action. • Create a UILocalNotification instance & configure it.
    12. 12. Creating a Notification • User performs an action. • Create a UILocalNotification instance & configure it. • Register the notification with the UIApplication instance.
    13. 13. Creating a Notification • User performs an action. • Create a UILocalNotification instance & configure it. • Register the notification with the UIApplication instance.
    14. 14. Creating a Notification • User performs an action. • Create a UILocalNotification instance & configure it. • Register the notification with the UIApplication instance.
    15. 15. Creating a Notification • User performs an action. • Create a UILocalNotification instance & configure it. • Register the notification with the UIApplication instance.
    16. 16. Receiving a Notification
    17. 17. Receiving a Notification • Some time passes…
    18. 18. Receiving a Notification • Some time passes… • If the device is locked, then we receive the notification bubble.
    19. 19. 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.
    20. 20. 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.
    21. 21. Receiving a Notification
    22. 22. Receiving a Notification • If the device is active and the application is not running, the notification bubble will appear.
    23. 23. 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
    24. 24. 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
    25. 25. Receiving a Notification
    26. 26. Receiving a Notification • If the application is in the foreground, then no notification is presented.
    27. 27. 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.
    28. 28. Adding Local Notifications
    29. 29. Creating a Notification
    30. 30. Creating a Notification • Alloc/init a UILocalNotification instance. • Configure its properties. • Register with the application’s UIApplication instance.
    31. 31. 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;
    32. 32. Creating a Notification
    33. 33. Creating a Notification • Scheduling Properties to set:
    34. 34. Creating a Notification • Scheduling Properties to set: • fireDate
    35. 35. Creating a Notification • Scheduling Properties to set: • fireDate • timeZone
    36. 36. Creating a Notification • Scheduling Properties to set: • fireDate • timeZone • repeatInterval
    37. 37. Creating a Notification • Scheduling Properties to set: • fireDate • timeZone • repeatInterval • repeatCalendar
    38. 38. Creating a Notification
    39. 39. Creating a Notification • Appearance Properties:
    40. 40. Creating a Notification • Appearance Properties: • alertBody
    41. 41. Creating a Notification • Appearance Properties: • alertBody • hasAction
    42. 42. Creating a Notification • Appearance Properties: • alertBody • hasAction • alertAction
    43. 43. Creating a Notification • Appearance Properties: • alertBody • hasAction • alertAction • alertLaunchImage
    44. 44. Creating a Notification
    45. 45. Creating a Notification • Other properties:
    46. 46. Creating a Notification • Other properties: • applicationIconBadgeNumber
    47. 47. Creating a Notification • Other properties: • applicationIconBadgeNumber • soundName
    48. 48. Creating a Notification • Other properties: • applicationIconBadgeNumber • soundName • userInfo
    49. 49. Scheduling
    50. 50. Scheduling • Get the UIApplication instance and call scheduleLocalNotification:
    51. 51. Scheduling • Get the UIApplication instance and call scheduleLocalNotification: [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    52. 52. Accessing Notifications • Call scheduledLocalNotifications
    53. 53. Cancelling Notifications
    54. 54. Cancelling Notifications • To cancel a single notification: • Call cancelLocalNotification:
    55. 55. Cancelling Notifications • To cancel a single notification: • Call cancelLocalNotification: • To cancel all notifications: • Call cancelAllLocalNotifications
    56. 56. Handling Notifications
    57. 57. Handling Notifications • Two entry points
    58. 58. Handling Notifications • Two entry points • Application is not running
    59. 59. Handling Notifications • Two entry points • Application is not running • Application is in foreground, or suspended
    60. 60. Handling Notifications
    61. 61. Handling Notifications • Not running?
    62. 62. Handling Notifications • Not running? • Check launchOptions dictionary?
    63. 63. 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; }
    64. 64. Handling Notifications • If we’re active, or have been backgrounded?
    65. 65. Handling Notifications - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { if ([application applicationState] == UIApplicationStateActive) { // Perform some custom notification logic } }
    66. 66. Other Resources • “Local and Push Notification Programming Guide” - developer.apple.com/iphone • “Session 129 - Implementing Local and Push Notifications” - WWDC 2010 videos

    ×