Search code examples
swiftpush-notificationapple-push-notificationsswiftui

turning off notifications at swiftUI


Apple document said I can turn off notifications by calling this func: unregisterForRemoteNotifications()

So I made a button which can call that function like this: Button("Hold") { unregisterForRemoteNotifications() }

But Xcode show me this error message 'Use of unresolved identifier 'unregisterForRemoteNotifications '

How can I fix this error? Thanks :)


Solution

  • This is a UIApplication instance method. You should call it via UIApplication.shared.unregisterForRemoteNotifications() So your code will look like:

    Button(action: {
        UIApplication.shared.unregisterForRemoteNotifications()
    }) {
        Text("Perform Action")
    }
    

    If you use UNUserNotificationCenter you could unsubscribe from pending notifications via: UNUserNotificationCenter.current().removeAllPendingNotificationRequests(), and to remove delivered use UNUserNotificationCenter.current().removeAllDeliveredNotifications()