Notification disappears after 5 seconds.

I would like the notification to stay.

I have inter alia following code, it works well but the notification disappears after 5 seconds (both when app is running or terminated):


@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

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

    let nc = UNUserNotificationCenter.current()
           
    nc.delegate = self
    return true
  }



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

    let request = notification.request
    let identifier = request.identifier

 
   let userInfo = notification.request.content.userInfo
 
        completionHandler([.banner, .list, .badge, .sound])
}
  • I only cited a part of the code here above, which is in the main App swift file (The creation of the notification is, as usual, in another file).

    I cited this part, because I presume, but do not know, that it has something to do with launchOptions or completionHandler.

    Hope someone has an idea.

Add a Comment

Accepted Reply

Hello :)

I think the completionHandler with the option .banner might be a problem. Banners disappear after a few seconds no matter if the application is running or not. I can't test it right now, but it might work with completionHandler([.alert, .badge, .sound]).

Replies

Hello :)

I think the completionHandler with the option .banner might be a problem. Banners disappear after a few seconds no matter if the application is running or not. I can't test it right now, but it might work with completionHandler([.alert, .badge, .sound]).

Thanks Timo23, your idea completionHandler([.alert, .badge, .sound]) pushed me in the right direction that it has to do with the characteristic of the banner. Although it did not really work to start with: I changed .banner to .alert but the notification still came as banner disappearing after some seconds. In the end I found out that my app has the following Settings page :

Tapping "Notifications" brings up following page:

where you can set the "Banner Style" to "persistent". Now the banner does not disappear anymore. One little disadvantage: the user has to perform this action, I have not found a way to do it programmatically.

Hello :)

okay, that‘s interesting, thank you for the information :) Maybe when you test it on another device it might work without performing that settings action. Furthermore you should completely delete and reinstall the application after changing the completionHandler method. Maybe you have done that already, otherwise you can try it. All in all there must be a way, because it has to work programmaticly.