I am writing my application in Swift for iOS8.
I wanted to use UILocalNotification to notify user of some things. For that, I have asked permission at the app launch with this:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
A confirm box shows up asking for permission. If user allows it, there's no problem.
If the user does not give permission, how do I check if local notifications are enabled from code (programmatically)?
..and this is how you check it in Swift:
let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
let required:UIUserNotificationType = UIUserNotificationType.Sound; // Add other permissions as required
let notification = UILocalNotification()
if (currentSettings.types & required) != nil {
println("sound will come and also vibration")
notification.soundName = UILocalNotificationDefaultSoundName
}else{
println("no sound no vibration")
}
Nice to know:
it will vibrate automatically, whether sound is on/off over your iphone button - nothing will happen if the user will set sound off in settings for your app.
So you don't have to add vibration additionally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With