2

Apple has approve my request for an entitlement to use critical alerts. I attached my entitlement to my bundle identifier and created a provisioning profile and manually signed my app according to Apple's instructions: https://help.apple.com/developer-account/#/dev38c81d4cd

I also added a properties.entitlements file with the correctly modified bundle identifier and boolean "YES". My "Build Settings" does not have a "Code Signing Entitlements" within "Signing".

When I run the code below, I get the prompt to accept alerts, but then no critical alert. Any suggestions? Thanks!

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var authOptions: UNAuthorizationOptions?
    if #available(iOS 12.0, *) {
        authOptions = [.alert, .badge, .sound, .criticalAlert]
    } else {
        authOptions = [.alert, .badge, .sound]
    }
    UNUserNotificationCenter.current().requestAuthorization(options:
      authOptions!) { (granted, error) in
        if !granted {
            print("The application requires Notifications permission to display push notifications. Please enable it in settings.")
        } else {
            
            let userNotificationCenter = UNUserNotificationCenter.current()
            
            var contentTitle:String?
            var contentMessage:String?
            var contentSound:UNNotificationSound?
            
            contentTitle = "Message:"
            contentMessage = "Critical Alert!"
            contentSound = .defaultCritical
            
            let content = UNMutableNotificationContent()
            content.title = contentTitle!
            content.subtitle = contentMessage!
            content.sound = contentSound
            
            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
            
            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            
            userNotificationCenter.add(request)
            
        }
    }
}

}

Pat Dolan
  • 41
  • 6
  • (1) These are local notifications, _not_ push notifications. (2) This has nothing to do with critical alerts; that's just a matter of whether sound plays. (3) You won't, by default, get any local notifications from your app when the app itself is frontmost. But it _is_ frontmost because only 1 second has elapsed. – matt Jun 04 '21 at 21:37
  • Thanks Matt. So critical alerts cannot be generated locally, but must be push notifications? – Pat Dolan Jun 05 '21 at 04:34
  • They can be local notifications but you have a comment in your code that suggests you think these are push notifications you're making. They are not. – matt Jun 05 '21 at 04:42
  • Can you direct me to some code for local critical alerts? Thanks. – Pat Dolan Jun 05 '21 at 05:51
  • I really don't know what you mean. I've explained why your local notification doesn't visibly do anything. It fires as scheduled, but you have not configured it so as to appear when your app is in the foreground. And your app _is_ in the foreground. – matt Jun 05 '21 at 05:53
  • @matt I don't think you understand what critical alerts are. They are a special entitlement granted by Apple which allows notifications to break thru DND mode. There is no obvious way in the notification API to mark notifications as critical – lewis Aug 18 '21 at 14:44
  • @PatDolan did you find a solution to this? – lewis Aug 18 '21 at 14:44
  • Lewis, no, I am still looking for a solution. Thanks for asking. – Pat Dolan Aug 19 '21 at 17:14
  • @lewis There is a way in the API to mark your notification as critical. You set the content sound to be a critical sound. Look at UNNotificationSound for the various critical sound options. We do this (we have the entitlement) and it works in a local notification. – chadbag Feb 25 '22 at 05:07

0 Answers0