How to know when ios local notification is triggered? - swift

Please tell me how can I know when local notification is trigger in Swift 4? I have tried everything it seem not working.

You can create local notification as
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: true)
let content = UNMutableNotificationContent.init()
content.title = "Danger Will Robinson"
content.subtitle = "Something This Way Comes"
content.body = "I need to tell you something, but first read this."
let notification = UNUserNotificationCenter.current()
notification.add(UNNotificationRequest.init(identifier: "notification", content: content, trigger: trigger), withCompletionHandler: nil)
When notification is triggered in background and user taps the notification.
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Swift.Void)
When notification is triggered and app is in foreground.
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Swift.Void)
You must confirm UNUserNotificationCenterDelegate to get the triggers.
So best approach is you initialise local notification in appDelegate.

Related

Notification permission request window does not appear in swift

My app has a function that pops up information in a notification in a few seconds when a button is clicked.
It used to work before, but these days it does not.
I checked and the permission request window does not appear.
Do I need to fix something? Code is below:
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound]) { (result, Error) in
print("result = \(result)")
}
AppDelegate:
UNUserNotificationCenter.current().delegate = self
func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions)
-> Void) {
completionHandler([.alert, .badge, .sound])
}

Swift UNNotificationRequest identifier of action button tapped local notification

Is it possible to get the UNNotificationRequest identifier of a tapped local notification button or any other information about the notification? Maybe via a delegate?
Thankyou
The identifier of the notification is obtained like so:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
let identifier = response.notification.request.identifier
switch response.actionIdentifier {
case "snoozeAction":
print ("Snooze Tapped - identifier :", identifier)
nc.post(name: Notification.Name("didTapSnooze"), object: nil)
default:
break
}
completionHandler()
}

Push Notification "willPresent" not called but "didReceive" works

I'm working on an app that handles push notifications.
For some reason,
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
Works as expected, but
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
is never called.
Anyone ever come across this situation?
All the usual suspects are in place:
UNUserNotificationCenter.current().delegate = self
is set in didFinishLaunchingWithOptions
let center = UNUserNotificationCenter.current()
UNUserNotificationCenter.current().delegate = self
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
center.requestAuthorization(options:options) { (granted, error) in
if error == nil{
DispatchQueue.main.async() {
UIApplication.shared.registerForRemoteNotifications()
completion(granted)
}
}
}
is returning true (permission granted)
Other items worth mentioning:
The Push notifications are sent via SendBird
When the app is in the background, everything works as expected.
Times and again, read the documentation.
Apple states for func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void):
Asks the delegate how to handle a notification that arrived while the app was running in the foreground.
That is, only when your app is running in FORGROUND and you get a notification will the system call this method to ask you whether this notification should still be shown to the user since they're already inside your app. You can call the completionHandler and pass in options like alert and sound so the user will still see the banner and hear the sound. If you only pass back sound, then the user sees no banner and only hears the notification sound.

Can you disable App-opening mechanism when the user presses the Notification Banner?

Using iOS13.4, XCode11.4, Swift5.2,
In my current App, I successfully managed to create a Local Notification. A banner is shown when the notification occurs. The banner is shown independently of App-state (i.e. opened App running in Foreground or Background or even if the App is completely closed). So far so good :)
Now my question: Can you disable App-opening mechanism when the user presses the Notification Banner ?
Currently, I am using the UNUserNotificationCenterDelegate's Delegate methods to complete the notification (i.e. run some custom code when the user presses the banner). (see code-snippet below)
However, I do not want the App to necessarily open up for doing the completion work ! This could be perfectly done in the background for better customer experience.
Is it possible to run some code when the banner gets pressed but do this without App opening ? And if yes, how ?
Here is the code snippet:
import UIKit
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let content = UNMutableNotificationContent()
content.title = "My Notification Title"
content.categoryIdentifier = UUID().uuidString
var notificationIdentifier = "23224-23134-234234"
var notificationDate = Date(timeInterval: 30, since: Date())
let notificationTriggerKind = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: notificationTriggerKind, repeats: false)
let notificationRequest = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: notificationTrigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.add(notificationRequest) {(error) in if let error = error { print("error: \(error)") } }
}
}
extension MyViewController: UNUserNotificationCenterDelegate {
// kicks in when App is running in Foreground (without user interaction)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
// ...do your custom code...
completionHandler([.alert, .sound])
}
// kicks in when App is running in Foreground or Background
// (AND App is open) AND user interacts with notification
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse, withCompletionHandler
completionHandler: #escaping () -> Void) {
// ... do your custom code ....
return completionHandler()
}
}

Is there a way a widget can catch UNUserNotificationCenterDelegate? SWIFT

Please help
UserNotifications
Import UserNotifications
put this in viewDidLoad
func setupNotif() {
UNUserNotificationCenter.current().requestAuthorization(options [.alert,.sound]) {
(granted, error) in
}
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Push Invitation"
notificationContent.subtitle = "Push subtitle"
notificationContent.body = "Please check notif"
notificationContent.sound = .default
notificationContent.categoryIdentifier = categoryIdentifier
let notificationTrigger = UNTimeIntervalNotificationTrigger.init(timeInterval: timeInterval, repeats: false)
let notificationRequest = UNNotificationRequest.init(identifier: "SampleNotif", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(notificationRequest) {(error) in if (error != nil) {} else { }}
}
this is the delegate for UUNotification center
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void) {
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
}
**My answer, there is no way to do that.. as per apple.. i think this question might pop up in the future..
#available(iOS 10.0, *)
open class UNUserNotificationCenter : NSObject {
// The delegate can only be set from an application
weak open var delegate: UNUserNotificationCenterDelegate?