I have a function that checks whether the user has enabled their push notification/ notification alert in the iphone. If the user has turned on their notification, it will print true in the swift view, and if the user has turned off their notification, it will print false.
I could achieve this using the stated below function and gives relevant output. But, this is not real-time. I have to close and re-open the app to reflect current changes. Im looking way to give true/false real-time and async using publisher and subscriber which show status realtime in my app
My code :-
var isRegister : Bool = false
func isEnabled() -> Bool {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in
if(settings.authorizationStatus == .authorized) {
isRegister = true
print("Push notification is enabled")
} else {
isRegister = false
print("Push notification is not enabled")
}
}
return isRegister
}
2
Answers
how to check in swift ui
Im trying to add text componenet to display text in my app. These solution does display in the log of xcode but no in the app. There is no way to use Text() as its bool value.
Any way to display text message on the screen directly and in the realtime
there are a delay in getting authorization status of notification
so you should have to use completion
and how to check
and if you want check status when click on Allow or Cancel of Notification Alert you can check it as follow
how to check in swift ui