Apparently this is now possible with ios10 :
optional func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
This answer basically says the tools needed to do it:
Displaying a stock iOS notification banner when your app is open and in the foreground?
I'm just not really understanding how to put it all together.
I dont know how important this is, but I'm not able to keep the optional func and xcode wants me to switch it to private.
I'm trying to show the badge, and the docs provide
static var badge: UNNotificationPresentationOptions { get }
Little lost here.
And then I'm assuming if I want to exclude a certain view controller from getting these badges and I'm not using a navigation controller this code I found would work? : var window:UIWindow?
if let viewControllers = window?.rootViewController?.childViewControllers {
for viewController in viewControllers {
if viewController.isKindOfClass(MyViewControllerClass) {
print("Found it!!!")
}
}
}
Swift 3 | iOS 10+
Assuming you know how to schedule a local notification:
You need to make your
AppDelegate
implement theUNUserNotificationCenterDelegate
protocol, and set it as the notification center's delegate withUNUserNotificationCenter.current().delegate = self
.Now your local notifications will appear when your app is in the foreground.
See the UNUserNotificationCenterDelegate docs for reference.
When your app is open in the foreground userNotificationCenter method call
Key to getting your notifications to show up while your app is in the foreground is also setting:
in your
UNNotificationRequest
. As for the rest, see the excellent answer by Rajan Maheshwari.There is a delegate method to display the notification when the app is open in iOS 10. You have to implement this in order to get the rich notifications working when the app is open.
In order to schedule a notification in iOS 10 and providing a badge
In order to register in AppDelegate we have to write this piece of code in
didFinishLaunchingWithOptions
I have defined actions also here. You may skip them
If you want that your notification banner should be shown everywhere in the entire application, then you can write the delegate of
UNUserNotificationDelegate
inAppDelegate
and make theUNUserNotificationCenter
current delegate toAppDelegate
Check this link for more details
https://www.youtube.com/watch?v=Svul_gCtzck
Github Sample
https://github.com/kenechilearnscode/UserNotificationsTutorial
Here is the output