UNUserNotifications never show up when app is in foreground

I have read everything I could, in Apple Dev, online and in the forums, and still cannot get the UNNotificationCenter to work...


This is the code I have:



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

print("Notification on app foreground")

completionHandler([.alert, .sound])

}


But it never shows up, I cannot get a breakpoint to break...

The code is in my AppDelegate.swift, and I have declared the "center" argument in didFinishLaunchingWithOptions.


private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

let center = UNUserNotificationCenter.current()

center.delegate = self

return true

}


I must be doing something wrong...it worked so well with UILocalNotifications 😟

When I print out my Trigger in the Console, I see this:


<UNNotificationRequest: 0x6000002203a0; identifier: FiveSecond, content: <UNNotificationContent: 0x6000000e8a80; title: Warning:, subtitle: (null), body: Don't go out tonight!, categoryIdentifier: myNotification, launchImageName: , peopleIdentifiers: (

), threadIdentifier: , attachments: (

), badge: 1, sound: <UNNotificationSound: 0x6000000b2ea0>, hasDefaultAction: YES, shouldAddToNotificationsList: YES, shouldAlwaysAlertWhileAppIsForeground: NO, shouldLockDevice: NO, shouldPauseMedia: NO, isSnoozeable: NO, fromSnooze: NO, darwinNotificationName: (null), darwinSnoozedNotificationName: (null), trigger: <UNTimeIntervalNotificationTrigger: 0x600000221420; repeats: YES, timeInterval: 60.000000>>


There doesn't seem to be a way to set this:

shouldAlwaysAlertWhileAppIsForeground: NO

Shouldn't it be YES?


Thank you for any help and/or advice!

Answered by Ragazzetto in 209447022

Try this when you create your UNMutableNotificationContent


Obj-C


UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
[content setValue:@YES forKey:@"shouldAlwaysAlertWhileAppIsForeground"];


Swift


content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

You need to implement the following for example in you app delegate

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    UNNotificationPresentationOptions presentationOptions =
    UNNotificationPresentationOptionSound
    | UNNotificationPresentationOptionAlert
    | UNNotificationPresentationOptionBadge;

    completionHandler(presentationOptions);
}

Don't forget to register the App delegate as delegate of the UNUserNotificationCenter

[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
Accepted Answer

Try this when you create your UNMutableNotificationContent


Obj-C


UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
[content setValue:@YES forKey:@"shouldAlwaysAlertWhileAppIsForeground"];


Swift


content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")

This was the missing link! Thank you 🙂

UNUserNotifications never show up when app is in foreground
 
 
Q