Why my didRecive and willPresent aren’t working(action buttons do nothing)?

104 Views Asked by At

I want to make an actionable notification. It appears only if my app is closed. When I press action button nothing happening

My class of notification

class Notfication : NSObject, UNUserNotificationCenterDelegate {

func request(answer : rope, RopesBas : RopesBase){
    let taskCategory = UNNotificationCategory(identifier:  "task", actions: [UNNotificationAction(identifier: "done", title: "Done")], intentIdentifiers: [])
    UNUserNotificationCenter.current().setNotificationCategories([taskCategory])
    print("0 stage")
    
    let content = UNMutableNotificationContent()
    content.title = answer.name
    content.sound =
    UNNotificationSound.default
    content.categoryIdentifier = "task"
    content.userInfo=["name" : answer.name]
    print("1 stage")
    
    // show this notification five seconds from now
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
    print("2 stage")
    
    //UNUserNotificationCenter.current().delegate = self
    // choose a random identifier
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    print("3 stage")
    
    // add our notification request
    UNUserNotificationCenter.current().add(request){ (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
    print("4 stage")
    print(UNUserNotificationCenter.current().delegate)
    print("ok")
}


func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("5 stage")
let userInfo = response.notification.request.content.userInfo
let id = "dad"
print("6 stage")
print(response.actionIdentifier)
switch response.actionIdentifier {
case "done":
    base.ropes.removeAll(where: {$0.name == id})
    break
case UNNotificationDefaultActionIdentifier,
UNNotificationDismissActionIdentifier:
    
    break
default :
    break
}
    print(base.ropes.count)
    completionHandler()
}

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

I’m declaring this class at sheet view

struct Adding: View {
private let publisher = Notfication()

And using it when I press the button in a sheet view

Button(action: {
                RopesDB.ropes.append(rope(name: answer.answer))
                publisher.request(answer: RopesDB.ropes.last!, RopesBas: RopesDB)
                dismiss()
            }, label: {
                HStack{
                    Spacer()
                    Text(answer.answer)
                    Spacer()
                }

I’ve tried to put delegate into

class AppDelegate: NSObject, UIApplicationDelegate{
internal func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    let center = UNUserNotificationCenter.current()
    let options : UNAuthorizationOptions = [.alert, .badge, .sound]
    center.requestAuthorization(options: options) { granted, error in
        if granted{
            UNUserNotificationCenter.current().delegate = Notfication()
        }
            if let error = error {
                print(error.localizedDescription)
            }
    }
    return true
}
//

} And into the class where it comment What my mistake? IDE Swiftplaygrounds(iPad 9, iOS 15.5)

2

There are 2 best solutions below

0
Plutonictoast1 On BEST ANSWER

I found out that this thing can’t work on Playgrounds, I suppose

1
JanMensch On

Apple states in its documentation that notifications are only shown when the app is in the background. When the app already is in the foreground, the notification will be directly passed into your app: https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions

You will need to receive the notification in your delegate's userNotificationCenter(_:willPresent:withCompletionHandler:) method and there your app can decide what to actually do with it. E.g. throw the user to a certain view, show a modal etc.