29

Hello I am trying to display some notification datas. In my notification I have a notification id to react deferently based on the pushed information.

I just cannot see what are those new UNNotificationPresentationOptions.

in my

userNotificationCenter(_: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

I have

 if notification.request.content.userInfo[keyName] as? String == "someId" {
     // Bla bla stuffs
     if #available(iOS 14.0, *) {
         completionHandler([.list, .banner, .sound])
     } else {
         completionHandler([.alert])
     }
  } else if ...

But when using [.banner] or [.list] or [.banner, .list] (without .alert) I just got nothing in foreground. In an ideal world I'd love to have the notification listed in the Notification center and display relevant information in my app (before calling completion handler).

Am I using thoses options right ?

FitzChill
  • 815
  • 8
  • 18

2 Answers2

54
  • [.list] will only show the notification in the notification center (the menu that shows when you pull down from the top)
  • [.banner] will only pop down a banner from the top like a normal push notification
  • [.list, .banner] will do both: show the banner and also make sure it's on the list.

[.alert] does essentially the same as having [.list, .banner], from what I can tell. They just added these two options to give more granular control.

I just tested this locally and it did what I expected, so if it doesn't work for you it may not be hitting those lines. Did you add some breakpoints to verify that those lines were getting hit?

johnny
  • 1,434
  • 1
  • 15
  • 26
  • After more debugging I finally realised that I was messing with iOS 14 specific code vs iOS 13 (with @available) after refactoring a bit I managed to understood more of what was happening and see the behaviour you are describing. – FitzChill Feb 01 '21 at 08:19
5

The notification shows in two ways

  1. It shows in the top menu (the menu that shows when you pull down from the top)
  2. In the notification center list

First, [.list] - It will show in the notification centre list, not in the top menu.

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

enter image description here

Second [.banner] - It will show in the top menu but not in the list like list

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

enter image description here

If you want both things list and top menu in the Notification Center then do this

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

It will give you both image output.

[.alert] does essentially the same as having [.list, .banner], from what I can tell. They just added these two options to give more granular control.

If you are looking for the code or tutorial then please check my video on Local Notification :- https://www.youtube.com/watch?v=yMbujKTf0uQ&t=1480s

Dharman
  • 30,962
  • 25
  • 85
  • 135
Yogesh Patel
  • 1,893
  • 1
  • 20
  • 55
  • 1
    "[.alert] does essentially the same as having [.list, .banner], from what I can tell. They just added these two options to give more granular control." Is literally a word for word copy from my answer. This seems like just a plug for your youtube channel. – johnny Oct 19 '21 at 23:50
  • please check that I added a code and screenshot. Not for the channel @johnnykehr. :-) – Yogesh Patel Oct 21 '21 at 03:41