Getting local notifications to show while app is in foreground Swift 3
Asked Answered
C

5

51

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!!!")
        }
    }
}
Cirrhosis answered 26/9, 2016 at 22:55 Comment(1)
Which method get executed when the app is in background and we receive the notification??Fascine
A
114

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.

extension ViewController: UNUserNotificationCenterDelegate {

    //for displaying notification when app is in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        //If you don't want to show notification when app is open, do something here else and make a return here. 
        //Even you you don't implement this delegate method, you will not see the notification on the specified controller. So, you have to implement this delegate and make sure the below line execute. i.e. completionHandler.

        completionHandler([.alert, .badge, .sound]) 
    }

    // For handling tap and user actions
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        switch response.actionIdentifier {
        case "action1":
            print("Action First Tapped")
        case "action2":
            print("Action Second Tapped")
        default:
            break
        }
        completionHandler()
    }

}

In order to schedule a notification in iOS 10 and providing a badge

override func viewDidLoad() {
    super.viewDidLoad()

    // set UNUserNotificationCenter delegate to self
    UNUserNotificationCenter.current().delegate = self
    scheduleNotifications()
}

func scheduleNotifications() {

    let content = UNMutableNotificationContent()
    let requestIdentifier = "rajanNotification"

    content.badge = 1
    content.title = "This is a rich notification"
    content.subtitle = "Hello there, I am Rajan Maheshwari"
    content.body = "Hello body"
    content.categoryIdentifier = "actionCategory"
    content.sound = UNNotificationSound.default

    // If you want to attach any image to show in local notification
    let url = Bundle.main.url(forResource: "notificationImage", withExtension: ".jpg")
    do {
        let attachment = try? UNNotificationAttachment(identifier: requestIdentifier, url: url!, options: nil)
        content.attachments = [attachment!]
    }      

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 3.0, repeats: false)

    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) { (error:Error?) in

        if error != nil {
            print(error?.localizedDescription ?? "some unknown error")
        }     
        print("Notification Register Success")
    }
}

In order to register in AppDelegate we have to write this piece of code in didFinishLaunchingWithOptions

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        registerForRichNotifications()
        return true
    }

I have defined actions also here. You may skip them

func registerForRichNotifications() {

       UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted:Bool, error:Error?) in
            if error != nil {
                print(error?.localizedDescription)
            }
            if granted {
                print("Permission granted")
            } else {
                print("Permission not granted")
            }
        }

        //actions defination
        let action1 = UNNotificationAction(identifier: "action1", title: "Action First", options: [.foreground])
        let action2 = UNNotificationAction(identifier: "action2", title: "Action Second", options: [.foreground])

        let category = UNNotificationCategory(identifier: "actionCategory", actions: [action1,action2], intentIdentifiers: [], options: [])

        UNUserNotificationCenter.current().setNotificationCategories([category])

    }

If you want that your notification banner should be shown everywhere in the entire application, then you can write the delegate of UNUserNotificationDelegate in AppDelegate and make the UNUserNotificationCenter current delegate to AppDelegate

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print(response.notification.request.content.userInfo)
        completionHandler()
    }

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

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

enter image description here

enter image description here

Aceous answered 27/9, 2016 at 3:12 Comment(9)
now suppose i want to set image from NSURL means web then how can i set this ?Apperceive
In iOS 10 works well but In iOS 9 How to show the notification over the app?Uterus
@Uterus you have to use some custom banner or BannerView or any third party which drops a view from top whenever a notification arrives.Aceous
@RajanMaheshwari Thanks for reply, is there any third party available?i am new to iOS.Uterus
Which method get executed when the app is in background and we receive the notification??Fascine
is it still working with iOS 13? i did not see its working.Overhead
How you this code is working? let url = Bundle.main.url(forResource: "notificationImage", withExtension: ".jpg") I am getting nil in url when passing image name and its extension from asset.Crapulent
@RajanMaheshwari, are we able to handle click on local push notification (without any action identifier) when an app is in an in-active state (killed state)?Jacobo
@KishanBhatiya didReceive is your delegate which is called whenever you tap on a local notification even without any action identifiers in whichever state your app inAceous
M
25

Swift 3 | iOS 10+

Assuming you know how to schedule a local notification:

func scheduleLocalNotification(forDate notificationDate: Date) {

    let calendar = Calendar.init(identifier: .gregorian)

    let requestId: String = "123"
    let title: String = "Notification Title"
    let body: String = "Notification Body"

    // construct notification content
    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: title, arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: body, arguments: nil)
    content.sound = UNNotificationSound.default()
    content.badge = 1
    content.userInfo = [
        "key1": "value1"
    ]

    // configure trigger
    let calendarComponents: [Calendar.Component] = [.year, .month, .day, .hour, .minute]
    let dateComponents = calendar.dateComponents(calendarComponents, from: notificationDate)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    // create the request
    let request = UNNotificationRequest.init(identifier: requestId, content: content, trigger: trigger)

    // schedule notification
    UNUserNotificationCenter.current().add(request) { (error: Error?) in
        if let error = error {
            // handle error
        }
    }
}

You need to make your AppDelegate implement the UNUserNotificationCenterDelegate protocol, and set it as the notification center's delegate with UNUserNotificationCenter.current().delegate = self.

// AppDelegate.swift

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions 
        launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // set app delegate as notification center delegate
        UNUserNotificationCenter.current().delegate = self
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {

    // called when user interacts with notification (app not running in foreground)
    func userNotificationCenter(_ center: UNUserNotificationCenter, 
        didReceive response: UNNotificationResponse, withCompletionHandler 
        completionHandler: @escaping () -> Void) {

        // do something with the notification
        print(response.notification.request.content.userInfo)

        // the docs say you should execute this asap
        return completionHandler()
    }

    // called if app is running in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent 
        notification: UNNotification, withCompletionHandler completionHandler: 
        @escaping (UNNotificationPresentationOptions) -> Void) {

        // show alert while app is running in foreground
        return completionHandler(UNNotificationPresentationOptions.alert)
    }
}

Now your local notifications will appear when your app is in the foreground.

See the UNUserNotificationCenterDelegate docs for reference.

Mantua answered 21/7, 2017 at 23:6 Comment(4)
awesome bro awesome! Charlie Sheen would say you were a rock star! If you have foreground notifications just copy and paste this snippet of code and you are done!.Edger
Which method get executed when the app is in background and app receive the notification??Fascine
@Fascine If your app is in the background and a notification appears on a user's device, no method is called. Only when a user interacts with a notification does the userNotificationCenter(didReceive response) method get called.Mantua
yep, assigning NUserNotificationCenter.current().delegate will break background push notifications (UIApplicationDelegate.didReceiveRemoteNotification() is no longer called)Posthorse
P
12

Key to getting your notifications to show up while your app is in the foreground is also setting:

content.setValue(true, forKey: "shouldAlwaysAlertWhileAppIsForeground")

in your UNNotificationRequest. As for the rest, see the excellent answer by Rajan Maheshwari.

Peterman answered 26/1, 2017 at 0:38 Comment(3)
You should NOT have to set this if your delegate for UNUserNotificationCenterDelegate and the registration of the action category are set up correctly. The first method in the delegate is there to process this app-in-foreground case. Setting this value is undocumented and may run foul of app store guidelines.Semifinal
@James, thanks, I've been investigating this in ObjC+iOS12 scenario and most answers don't actually clarify on the correctness of the code, assuming the line is necessary and fixing the line to work, rather than determining if it's needed or actually the recommended approach.Corneille
This stuff is no longer valid - the app crashes with an error - SetValueForUndefinedKey: "shouldAlwaysAlertWhileAppIsForeground"Vashtivashtia
H
6

When your app is open in the foreground userNotificationCenter method call

func userNotificationCenter(_ center: UNUserNotificationCenter,
   willPresent notification: UNNotification, 
   withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler(.alert)
}
Haase answered 1/12, 2018 at 11:43 Comment(0)
P
1

None of these answers are good with recent IOS versions

  1. shouldAlwaysAlertWhileAppIsForeground will crash on >= IOS 12

  2. assigning UNUserNotificationCenter.current().delegate changes the behavior of background push notifications. UIApplicationDelegate.didReceiveRemoteNotification() is no longer called, when push notification is received and app is on background (until user clicks the notification).

Posthorse answered 15/9, 2021 at 10:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.