2

I've created a scheduled notification and I'd like to be able to do stuff once it gets delivered. Not once it's clicked on or selected, but when it's actually delivered and shows up on the user's screen.

The code I've used to generate the notification is:

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "foo", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "bar", arguments: nil)


var dateInfo = DateComponents()
dateInfo.hour = 7
dateInfo.minute = 0
print(dateInfo.hour!)
print(dateInfo.minute!)

let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
// Create the request object.
let notificationRequest = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
center.add(notificationRequest)

I'm sure I've got to add something to my AppDelegate that'll respond when the notification is delivered, but I've been looking all over the internet and can't find a way to do it on delivery rather than on select.

Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
IanCZane
  • 600
  • 4
  • 21

1 Answers1

2

When a notification arrives, the system calls the userNotificationCenter(_:willPresent:withCompletionHandler:) method of the UNUserNotificationCenter object’s delegate.

 let center = UNUserNotificationCenter.current()  
 center.delegate = self // What delegation target Here is my AppDelegate




extension AppDelegate : UNUserNotificationCenterDelegate {
               // while your app is active in forground

             // Handle Notifications While Your App Runs in the Foreground

             func userNotificationCenter(_ center: UNUserNotificationCenter,
                                            willPresent notification: UNNotification,
                                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
                    let userInfo = notification.request.content.userInfo

                    // Change this to your preferred presentation option
                    // Play a sound.
                    //  completionHandler(UNNotificationPresentationOptions.sound)
            }

              // While App is inactive  in background
                func userNotificationCenter(_ center: UNUserNotificationCenter,
                                            didReceive response: UNNotificationResponse,
                                            withCompletionHandler completionHandler: @escaping () -> Void) {
                    let userInfo = response.notification.request.content.userInfo

                    // While App is inactive  in background


                    print(userInfo)


                    completionHandler()
                }
        }
Abdelahad Darwish
  • 5,969
  • 1
  • 17
  • 35
  • Does this go in AppDelegate or as a separate class? – IanCZane May 07 '18 at 18:54
  • your delegation target let center = UNUserNotificationCenter.current() center.delegate = self // What delegation target @IanCZane – Abdelahad Darwish May 07 '18 at 18:55
  • Okay - so this can go in its own file or within AppDelegate.swift as long as I have the top two lines in the same file? – IanCZane May 07 '18 at 19:02
  • The actions only fire when the notification is tapped on, not when it's delivered. I added some print statements and they fire just fine, but not when the notification is delivered - only when tapped. – IanCZane May 07 '18 at 19:06
  • **willPresent notification: UNNotification** will fire when notification is delivered while your app is active , when app in background you should click on notification to fire it – Abdelahad Darwish May 07 '18 at 19:07
  • Okay, but is it possible to get it to fire on delivery if the app is in the background rather than the foreground? I understand that I can handle delivery in the foreground, but I need to be able to also handle it in the background. – IanCZane May 07 '18 at 19:12
  • 1
    No way for local notification to fire on delivery while app in background , there is silent remote notification used to return killed app to background to perform task for 30 sec usually update content task – Abdelahad Darwish May 07 '18 at 19:15