Mars
Mars

Reputation: 936

ios check for displayed local notifications

assume I scheduled 3 local notifications and when users opens app, three notifications have following states:

a) not displayed yet
b) displayed
c) displayed and user taps on this notification and come back to app.

How do i detect each one?

Upvotes: 1

Views: 605

Answers (1)

Paulw11
Paulw11

Reputation: 114826

iOS 10 has introduced the UIUserNotificationCenter, and deprecated the scheduling and delivery of local notifications via UIApplication, so there are some differences depending on which method you are using. The deprecated methods still work on iOS 10, so if you are targeting iOS earlier than iOS10 then you can continue to use these methods, but you will get deprecation warnings.

For case a, a notification that hasn't yet been delivered, details of this notification are available by calling getPendingNotificationRequests on an instance of UIUserNotificationCenter (iOS 10) or by accessing the scheduledLocalNotifications property on your app's UIApplication instance (Prior to iOS 10)

For case b, a notification that has been delivered but that the user didn't interact with, no information is available.

For case c, the notification that the user tapped to launch the app, it depends on the state of the application and how the user interacts with the notification.

  • If the app is not running in the foreground or suspended, then the application is launched and the notification payload is delivered to the application:didFinishLaunchingWithOptions: via the localNotification key in the options dictionary
  • If the app is suspended, then the notification is delivered to didReceiveLocalNotification delegate method is called.
  • If the notification has custom actions and the user taps one of those then the application:handleActionWithIdentifier:forLocalNotification:completionHandler: app delegate method is called

Upvotes: 1

Related Questions