Handling notifications in your app

Incoming notifications are most often received when the user is not using your app. This means that your app is not directly aware of the incoming notification, and doesn't get to handle it until the user taps the notification to open your application.

When your application launches due to a tapped notification, the UNUserNotificationCenterDelegate delegate protocol that belongs to the UNUserNotificationCenter object is called. This delegate is responsible for handling the incoming notification whether the app is currently in the foreground, background, or not active at all, perhaps because it's been killed by the system or force-closed by the user.

You are expected to assign a UNUserNotificationCenterDelegate delegate before the app has finished launching. This means that you should set the delegate as soon as possible, so application(_:didFinishLaunchingWithOptions:) is a great candidate for setting the delegate.

When the user has tapped a notification, or selects one of the custom actions that is attached to the notification, the userNotificationCenter(_:didReceive:withCompletionHandler:) method is called on the notification center delegate. If the app is in the foreground and a notification is received, userNotificationCenter(_:willPresent:withCompletionHandler:) is called right before the notification is shown to the user. You can use this method to determine whether the notification should be shown to the user. Sometimes, it might not make sense to present a certain notification to the user if the app is already running. For instance, you wouldn't show a notification for an incoming message if the user is already looking at the new message.

Let's see what the notification center delegate methods look like when they are implemented. First, import the UserNotifications framework in AppDelegate and add the following code to implementation for application(_:didFinishLaunchWithOptions:) to set the app delegate as a delegate for the UNUserNotificationCenter:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  // existing code...

  UNUserNotificationCenter.current().delegate = self

  return true
}

Next, add the following extension to AppDelegate.swift to make the app delegate conform to UNUserNotificationCenterDelegate:

extension AppDelegate: UNUserNotificationCenterDelegate {
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let notification = response.notification
    let action = response.actionIdentifier

    let notificationTitle = notification.request.content.title
    let customAttributes = notification.request.content.userInfo

    completionHandler()
  }

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.alert, .sound])
  }
}

Both delegate methods have access to the original notifications and the notification contents. You can use this information to determine exactly which actions should be taken by your app to properly handle the notification. When a notification is tapped, userNotificationCenter(_:didReceive:withCompletionHandler:) is called. This method receives a UNNotificationResponse object, which provides information about the selected action, in addition to the notification information.

If your app receives a notification and it's in the foreground, userNotificationCenter(_:willPresent:withCompletionHandler:) allows you to determine what should happen with the notification. This method is passed a closure that you need to call to inform iOS about what should happen with the notification. If you want to handle the notification yourself, you can simply call the closure without any arguments. In the preceding snippet, the desired behavior is to display all notification alerts to the user and play the corresponding sounds.

Because you can access the original notification, you can determine whether the notification should be shown for each incoming notification individually, depending on the notification content, category, or custom attributes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset