Reputation: 20058
I have a local notification that is triggered based on a date. When this notification is triggered I do some action.
The way I do it right now is this way:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
print("Dismiss Action")
case UNNotificationDefaultActionIdentifier:
print("Default")
// Do Action Here
case "Snooze":
print("Snooze")
case "Delete":
print("Delete")
default:
print("Unknown action")
}
completionHandler()
}
This is called when I take action on the notification that appears on my screen.
What I would like is, if I am inside the app the notification should not appear and perform my task automatically.
Is this possible ?
Right now my task is performed only when pressing on the notification received. I don't think it makes much sense if I am inside the app.
Upvotes: 0
Views: 170
Reputation: 19156
Implement userNotificationCenter:willPresent
delegate method of UNUserNotificationCenter
and pass empty array as options to don't display notification.
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
// Use this to avoid notification to appear on screen
completionHandler([])
// Use this to dispaly notification
completionHandler([UNNotificationPresentationOptions.alert, .sound, .badge])
}
Upvotes: 2