UNUserNotificationCenter didReceive response not called when app is terminated - ios

I'm working on local notifications but the problem I have is that the method didReceive Response is not being called when the app is terminated so when I tap on a notification action it just launches the app and did nothing else. But when the app is just in the background everything works as usual. Anything wrong with my code?
//MyClassNameViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().delegate = self
}
func triggerAlarm1() {
// Create an instance of notification center
let center = UNUserNotificationCenter.current()
// Sets the details of the notification
let content = UNMutableNotificationContent()
content.title = "Recorded Today's first alarm."
content.body = "Be completely honest: how is your day so far?"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "notificationID1"
// Set the notification to trigger everyday
let triggerDaily = Calendar.current.dateComponents([.hour,.minute], from: myTimePicker1.date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
// Deliver the notification
let identifier = "UYLLocalNotification"
let request = UNNotificationRequest(identifier: identifier,
content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if error != nil {
// Just in case something went wrong
print(error!)
}
})
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("didReceive Method called")
if response.actionIdentifier == "actionOne" {
let alertOne = UIAlertController(title: "First", message: "Some Message Here", preferredStyle: UIAlertControllerStyle.alert)
let actionOne = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
alertOne.addAction(actionOne)
self.present(alertOne, animated: true, completion: nil)
}
completionHandler()
}
//AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().delegate = self
// Request Authorisation
UNUserNotificationCenter.current().requestAuthorization(options: [.alert , .sound , .badge]) { (Bool, error) in
// insert code here
}
let actionOne = UNNotificationAction(identifier: "actionOne", title: "Open1", options: [.foreground])
let catogeryOne = UNNotificationCategory(identifier: "notificationID1", actions: [actionOne], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([catogeryOne])
return true
}

Call this function inside of your action identifier and you'll be ok!
func alertAction() {
let alertController = UIAlertController(title: "Hello", message: "This is cool!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
// Do something with handler block
}))
let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]
presentedViewController.present(alertController, animated: true, completion: nil)
}
It's super easy!
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("didReceive Method called")
if response.actionIdentifier == "actionOne" {
DispatchQueue.main.async(execute: {
self.alertAction()
})
} else if response.actionIdentifier == "actionTwo" {
} else if response.actionIdentifier == "actionThree" {
}
completionHandler()
}
Fully works on Swift 3.0 and Xcode 8.0. I have changed all of the connections between the view controller. I've added a NavigationController to the initial ViewController.
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.delegate = self
// Request Authorisation
center.requestAuthorization(options: [.alert , .sound , .badge]) { (Bool, error) in
// insert code here
}
let actionOne = UNNotificationAction(identifier: "actionOne", title: "Open1", options: [.foreground])
let catogeryOne = UNNotificationCategory(identifier: "notificationID1", actions: [actionOne], intentIdentifiers: [], options: [])
let actionTwo = UNNotificationAction(identifier: "actionTwo", title: "Open2", options: [.foreground])
let catogeryTwo = UNNotificationCategory(identifier: "notificationID2", actions: [actionTwo], intentIdentifiers: [], options: [])
let actionThree = UNNotificationAction(identifier: "actionThree", title: "Open3", options: [.foreground])
let catogeryThree = UNNotificationCategory(identifier: "notificationID3", actions: [actionThree], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([catogeryOne, catogeryTwo, catogeryThree])
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent method called")
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("didReceive Method called")
if response.actionIdentifier == "actionOne" {
DispatchQueue.main.async(execute: {
self.alertAction()
})
} else if response.actionIdentifier == "actionTwo" {
} else if response.actionIdentifier == "actionThree" {
}
completionHandler()
}
func alertAction() {
let alertController = UIAlertController(title: "Hello", message: "This is cool!", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
// Do something with handler block
}))
let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]
presentedViewController.present(alertController, animated: true, completion: nil)
}
Also I have deleted all of the previous suggestions from the viewDidLoad and other places.
Change your connections to the show and do not present as modally. If you want to show your alerts everywhere. Good luck

I have Conformed to UNUserNotificationCenterDelegate inside AppDelegate. Everything is simple just posted my notification inside async block that will run after 0.1 second.
I am observing Notification.Name.LocalNotificationTapped inside my HomeViewController to update UI whenever this notification is observed For Example to present some popup etc
You can think about the code inside async block like you are presenting any alert or presenting any screen.
UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let notificationData = response.notification.request.content.userInfo
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
NotificationCenter.default.post(name: Notification.Name.LocalNotificationTapped, object: nil,userInfo: notificationData)
}
completionHandler()
}

Related

UNUserNotificationCenterDelegate not called XCode Swift

UNUserNotificationCenterDelegate functions are not called when receiving local notifications in ios (swift). Also, I am unable to receive notifications in foreground.
Here is my AppDelegate class
let notificationCenter = UNUserNotificationCenter.current()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
DBManager.createEditableCopyOfDatabaseIfNeeded()
notificationCenter.delegate = self
let options: UNAuthorizationOptions = [.alert, .sound, .badge]
notificationCenter.requestAuthorization(options: options) {
(didAllow, error) in
if !didAllow {
print("User has declined notifications")
}
}
return true
}
}
App Delegate Extension for UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print(response.notification.request.content.userInfo)
completionHandler()
}
func scheduleNotification(_ body: String,_ date:Date) {
let identifier = self.convertDateInMiliseconds(date: date)
let content = UNMutableNotificationContent()
content.title = "TEST APP"
content.body = body
content.sound = UNNotificationSound.default
content.badge = 1
content.categoryIdentifier = "\(identifier)1234"
let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let request = UNNotificationRequest(identifier: "\(identifier)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error == nil {
print("TRUE")
} else {
print("FALSE")
print(error?.localizedDescription ?? "ERROR")
}
}
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "DeleteAction", title: "Delete", options: [.destructive])
let category = UNNotificationCategory(identifier: "\(identifier)1234",
actions: [snoozeAction, deleteAction],
intentIdentifiers: [],
options: [])
notificationCenter.setNotificationCategories([category])
}
func convertDateInMiliseconds(date: Date) -> Int {
let since1970 = date.timeIntervalSince1970
return Int(since1970 * 1000)
}
}
I am using iOS 14, swift 5.0 and xcode 11.5.
I am receiving notifications in background but not in foreground.
Thanks.
Try this one please
func scheduleNotification(_ body: String,_ date:Date) {
let identifier = self.convertDateInMiliseconds(date: date)
let content = UNMutableNotificationContent()
content.title = "TEST APP"
content.body = body
content.sound = UNNotificationSound.default
content.badge = 1
content.categoryIdentifier = "\(identifier)1234"
let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute, .second,], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly, repeats: true)
let center = UNUserNotificationCenter.current()
center.delegate = self
let request = UNNotificationRequest(identifier: "\(identifier)", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if error == nil {
print("TRUE")
} else {
print("FALSE")
print(error?.localizedDescription ?? "ERROR")
}
}
let snoozeAction = UNNotificationAction(identifier: "Snooze", title: "Snooze", options: [])
let deleteAction = UNNotificationAction(identifier: "DeleteAction", title: "Delete", options: [.destructive])
let category = UNNotificationCategory(identifier: "\(identifier)1234",
actions: [snoozeAction, deleteAction],
intentIdentifiers: [],
options: [])
notificationCenter.setNotificationCategories([category])
}

Display Firebase Notification Popup in swift 4

Notice: Solution at the bottom.
Original issue.
I followed the directions on firebase to set up push notifications in iOS. I do successfully get notification to the phone and will display at the top of the phone if the app is closed, but if I'm in the app the notification never displays. I see the print out in the terminal though so I know its receiving the notification.
Ideal functionality would be:
1) if app is closed or in background when the user clicks the notification it opens the app then a in app popup alert will display with the notification with a Okay button to click
2) if the app is in the foreground the notification displays as a popup alert with a Okay button to click.
Below is the AppDelegate.swift code which when I run it and send a notification, I get the notification but I also get the following error when trying to display the alert. Warning: Attempt to present UIAlertController on Company.AuthorizationCheckViewController whose view is not in the window hierarchy!
Thank you for any help you can provide.
import UIKit
import GoogleMaps
import Sentry
import UserNotifications
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let userDefaults = UserDefaults.standard
FirebaseApp.configure()
Messaging.messaging().delegate = self
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
}
func applicationDidEnterBackground(_ application: UIApplication) {
}
func applicationWillEnterForeground(_ application: UIApplication) {
VersionCheck.shared.IsUpdateRequired()
}
func applicationDidBecomeActive(_ application: UIApplication) {
}
func applicationWillTerminate(_ application: UIApplication) {
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)")
}
let message : [String : Any] = userInfo["aps"] as! [String : Any]
let messageAlert : [String : Any] = message["alert"] as! [String : Any]
let lBody : String = messageAlert["body"] as! String
let lTitle : String = messageAlert["title"] as! String
print("body 1 = \(lBody)") //this works!
print("title = \(lTitle)") //this works!
let alert = UIAlertController(title: lTitle, message: lBody, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil);
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
let message : [String : Any] = userInfo["aps"] as! [String : Any]
let messageAlert : [String : Any] = message["alert"] as! [String : Any]
let lBody : String = messageAlert["body"] as! String
let lTitle : String = messageAlert["title"] as! String
print("body 2 = \(lBody)")
print("title = \(lTitle)")
let alert = UIAlertController(title: lTitle, message: lBody, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil);
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
completionHandler(UIBackgroundFetchResult.newData)
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Unable to register for remote notifications: \(error.localizedDescription)")
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("APNs token retrieved: \(deviceToken)")
}
}
#available(iOS 10, *)
extension AppDelegate : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = notification.request.content.userInfo
let message : [String : Any] = userInfo["aps"] as! [String : Any]
let messageAlert : [String : Any] = message["alert"] as! [String : Any]
let lBody : String = messageAlert["body"] as! String
let lTitle : String = messageAlert["title"] as! String
print("body 3 = \(lBody)")
print("title = \(lTitle)")
let alert = UIAlertController(title: lTitle, message: lBody, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
completionHandler([UNNotificationPresentationOptions.alert])
}
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: #escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
if let messageID = userInfo["gcm.message_id"] {
print("Message ID: \(messageID)") //can use the id later to privent multiple popups of the same message
}
let message : [String : Any] = userInfo["aps"] as! [String : Any]
let messageAlert : [String : Any] = message["alert"] as! [String : Any]
let lBody : String = messageAlert["body"] as! String
let lTitle : String = messageAlert["title"] as! String
print("body 4 = \(lBody)")
print("title = \(lTitle)")
let alert = UIAlertController(title: lTitle, message: lBody, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)
self.window?.rootViewController?.present(alert, animated: true, completion: nil)
completionHandler()
}
}
extension AppDelegate : MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)whenever a new token is generated.
}
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
print("Received data message: \(remoteMessage.appData)")
}
}
Solution:
I added PresentedViewController? to the code and it allowed the alert popup to display.
Also by adding UIBackgroundFetchResults.newData to the completionHandler I was able to have the notification displayed in the app
new code looks like this
let alert = UIAlertController(title: lTitle, message: lBody, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.window?.rootViewController?.presentedViewController?.present(alert, animated: true, completion: nil)
completionHandler(UIBackgroundFetchResult.newData)

UNNotificationAction buttons not showing,Why?

I refer the code from Apple, but not working, here's my code:
override func viewDidLoad() {
super.viewDidLoad()
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert,.sound]) { (granted, error) in
print(granted)
}
//Set content
let content = UNMutableNotificationContent()
content.title = "Hello World"
content.body = "My First Notification App"
content.sound = UNNotificationSound.default()
//Set trigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
//Set request
let uuid = UUID().uuidString
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
//Set actions
let blueAction = UNNotificationAction(identifier: "ACTION_BLUE", title: "Blue", options: .init(rawValue: 0))
let greenAction = UNNotificationAction(identifier: "ACTION_GREEN", title: "Green", options: .init(rawValue: 0))
let category = UNNotificationCategory(identifier: "COLOR_ACTION", actions: [blueAction,greenAction], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "", options: .customDismissAction)
center.setNotificationCategories([category]) //category
center.add(request) { (err) in
if err != nil {
print(err)
}
}
// Do any additional setup after loading the view, typically from a nib.
}
here is UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if response.notification.request.content.categoryIdentifier == "COLOR_ACTION" {
}
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
I think noting was wrong, but it isn't work. Hope someone could help me, I am really confused now lol.
You need add the content.categoryIdentifier = "COLOR_ACTION" before the call of UNNotificationRequest like the code below in swift.
//Set content
let content = UNMutableNotificationContent()
content.title = "Hello World"
content.body = "My First Notification App"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "COLOR_ACTION"
Here is the output when you drag the notification:

Attempt to present UIAlertController whose view is not in the window hierarchy with localnotification [duplicate]

This question already has answers here:
Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
(38 answers)
Closed 6 years ago.
I am trying to present AlertView once the user clicks on the local notification. The AlertView has options of cancel or ok.
extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("Tapped in notification")
if(defaults.object(forKey: "alertOn") != nil){
// Create the alert controller
let alertController = UIAlertController(title: "Some text", message: "Some text again", preferredStyle: .alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
UIAlertAction in
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)
// Present the controller
self.present(alertController, animated: true, completion: nil)
}
}
func triggerNotification(){
print("notification will be triggered in five seconds..Hold on tight")
let content = UNMutableNotificationContent()
content.title = "SomeText"
content.body = "Some more text"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription)
}
}
}
It should show me the alertview with option to ok or cancel a request. Instead its showing me message UIAlertController whose view is not in the window hierarchy
When I put the alertview in viewdidapear it works fine but when I put it in userNotificationCenter my AlertView not get attached to the main view.
Brief View of Code
extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
Present UIAlertView
}
}
ViewController{
Call to notification when app is in background
triggerNotification()
triggerNotification(){
Definition
}
}
try this
func currentTopViewController() -> UIViewController {
var topVC: UIViewController? = UIApplication.shared.delegate?.window?.rootViewController
while topVC?.presentedViewController {
topVC = topVC?.presentedViewController
}
return topVC!
}
and present the VC as
let currentTopVC: UIViewController? = self.currentTopViewController()
currentTopVC.present(alertController, animated: true, completion: nil)

(ios10.2)(swift3) How to tell if a user has clicked on a local notification?

I made an app that sends a notification after a certain time when you click a button. This notification was created in the ViewController. How do I make my app do something after the User clicks the notification? I am using swift 3 and not using UILocalNotification.
In your app delegate, configure an object to be the user notification center's UNUserNotificationCenterDelegate and implement userNotificationCenter(_:didReceive:withCompletionHandler:).
Note that if the user merely dismisses the notification alert, this method will not be called unless you have also configured a category (UNNotificationCategory) corresponding to this notification, with the .customDismissAction option.
UILocalNotification is deprecated in iOS 10. You should use UserNotifications framework instead.
Before all don't forget import UserNotifications
First you should set up UNUserNotificationCenterDelegate.
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
// - Handle notification
}
}
Than set UNUserNotificationCenter's delegate.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (accepted, _) in
if !accepted {
print("Notification access denied.")
}
}
return true
}
Now you can setup your notification.
func setup(at: Date) {
let calendar = Calendar.current
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current,
month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "Just a reminder"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
And finally handle it!
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
if response.notification.request.identifier == "textNotification" {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let rootVc = appDelegate.window?.rootViewController else { return }
let alert = UIAlertController(title: "Notification", message: "It's my notification", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
rootVc.present(alert, animated: true, completion: nil)
}
}
}

Resources