from apple

In iterative iOS releases apple incorporated new features into the iOS notification system.The aim of the new features was to provide a more immersive and interactive experience for the user within the notification.Thus users would be able to get more information and also perform critical action right within the notification without the need of opening the app.

The most prominent features among them are:

  1. Notification Actions: provides the actions that the user can perform to interact with the notification message. They are presented as buttons. Available via UIUserNotificationAction.
  2. Text Input in Notification Actions: Provides the user the ability to input text within a notification.Implemented via UNTextInputNotificationAction.
  3. Grouped Notifications: Ability to group similar notification into a single group so that the notification center remains clean and also to provide more control and information to the user.iOS automatically groups notifications or you can implement your custom grouping via threadIdentifier.
  4. Silent Notifications: Can be used to update contents in the app silently in the background.These type of notifications does not display an alert, play a sound, or badge your app’s icon.Implemented via content-available key.
  5. Notification Attachments: Attach audio, image, or video content together in an alert-based notification.Implemented via UNNotificationAttachment and UNNotificationServiceExtension.
  6. Custom Notification UI: Customise the UI of the notification.Implemented via UNNotificationContentExtension.
  7. Interactive Controls in Custom UI: Ability to add custom controls within the UI so that users can interact within the notifications.

Playing Youtube Video within a Notification

Among the capabilities of push notification in iOS ,you can add custom UI
to a notification wherein you can provide custom content including text,audio,video and gifs.These features are implemented using UNNotificationServiceExtension and UNNotificationContentExtension as noted above.To play a video you can either download the video and provide it as a UNNotificationAttachment or you can play a web link using AVPlayer. But you can’t play a youtube video using these methods .Here I am going to explain how I enabled playing youtube videos within a notification.

To enable playing youtube videos within an iOS aspplication,google provides a library called youtube-ios-player-helper, which helps you embed a YouTube iframe player into an iOS application. The library creates a UIWevView and a bridge between your application’s code and the YouTube player’s JavaScript code, thereby allowing the iOS application to control the YouTube player.

Here we combine the UNNotificationContentExtension(which is used to create custom UI for notification) and youtube-ios-player-helper to enable playingyoutube videos within a notification.

So first we need to add a notification content app extension to the project.Then you specify the category for the particular extension in its Info.plist.This category names is used to determine whether the content extension is to be used when a notification arrives.

The created extension consists of a Storyboard file(which contains the view controller to be used as our custom UI) and a ViewController class which conforms to UNNotificationContentExtension protocol.You can configure the custom UI when the notification arrives using the didReceive(_ notification: UNNotification) method.

To support playing youtube video we need to

  1. Install youtube-ios-player-helper as a pod

  2. Import youtube_ios_player_helper in our extensions viewcontroller class.

  3. Add a view to our viewcontroller in storyboard and set its class as YTPlayerView and then add an outlet(this view enables playing youtube video)

  4. Then in the didReceiveNotification method we extract the video url from the notification payload(which needs to be added as a custom parameter).

    func didReceive(_ notification: UNNotification) {
    
    if let attachmentURL = notification.request.content.userInfo["video-url"] as? String, let url
    = URL(string: attachmentURL){
    
    ytPlayerView.isHidden = false
    
    ytPlayerView.delegate = self
    
    if let linkID = extractYoutubeIdFromLink(link: attachmentURL){
    
    let playerVars = [ "playsinline" : 1]
    
    self.ytPlayerView.load(withVideoId: linkID, playerVars: playerVars)
    
       }  
      }  
    }
    
  5. Then extract the youtubeId from the url using regEx.

     func extractYoutubeIdFromLink(link: String) -> String? {
     
     let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
     guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
    
     return nil   }
    
     let nsLink = link as NSString
    
     let options = NSRegularExpression.MatchingOptions(rawValue: 0)
    
     let range = NSRange(location: 0, length: nsLink.length)
    
     let matches = regExp.matches(in: link as String, options:options, range:range)
    
     if let firstMatch = matches.first {   return nsLink.substring(with: firstMatch.range)   }   return nil   }
    
  6. Load the video using ytPlayerView’s loadVideoWithId method and set ytPlayerView outlets delegate to self.

self.ytPlayerView.load(withVideoId: linkID, playerVars: playerVars)

  1. When the ytPlayerView becomes ready to play the loaded video it informs us through the delegate method playerViewDidBecomeReady(_ playerView: YTPlayerView_)._In the delegate method we start playing the video
 func playerViewDidBecomeReady(**_** playerView: YTPlayerView) {  
           	playerView.playVideo()  
       }

Now when the notification arrives and the user selects the notification by dragging the didReceive method is called and the youtube video starts to play.
And we are done.Hip Hip Hooray!.