What do you do when you don't have an entitlements file already?
There are several sources that can help you implement critical alerts for your iOS app. I found two that were very helpful:
https://www.tapcode.co/2018/10/17/how-to-implement-critical-alerts-in-ios-12/
Both describe what is necessary to request approval from Apple and how to update your project to include critical alerts notifications – with one exception. The articles explain how to update your entitlements file to add the critical alerts key. But what do you do when your app currently does not have an entitlements file? After a bit of research, I arrived at the following steps that can be used as a guide for dealing with this dilemma. When you get to the part of the articles that tackle updating the entitlements file please proceed as follows:
- Under the Capabilities tab, click on + (plus) to add a capability (I picked Keychain Sharing). This will cause Xcode to generate a .entitlements file (major hurdle overcome).
- Click on that file in the Project Navigator to open it. You will see the key for the capability that you have chosen (Keychain Sharing) if the disclosure triangle next to Entitlements File is open. If the disclosure triangle is not open, please click it to open it.
- Click on + (plus) next to Entitlements File to add an entitlement. A dropdown with selections will appear but you will have to type the key for critical alerts notifications in order to enter it manually.
- Enter the key for critical alerts notifications that is in the articles: com.apple.developer.usernotifications.critical-alerts
- Change the type to Boolean and select YES as the value.
- Click on – (dash) next to the capability that you used to create the entitlements file in step 1 (Keychain Sharing) to delete it. You now have an entitlements file with the critical alerts key that you need.
- Build and run your app and you should see the prompt that asks the user to allow critical alerts.
Mission accomplished!
Terry Chaisson
GUIDE OF HOW TO IMPLEMENT IOS CRITICAL ALERTS [JULY 2021]
First Steps
You need to request a Critical Alert Notifications Entitlement on the Apple website.
Then you need to wait for the approval of your request from the Apple side. They will send you an email. In my case, I waited only 3 days. Some developers waited for answers for around 1-4 weeks.
WHAT DO YOU NEED TO DO AFTER REQUEST APPROVAL?
Open the Identifiers page on developer.apple.com and select the identifier of the application where you are want to enable critical alerts.
On the page that opens, click «Additional features» and tick the «Critical Alerts» box. Then click the«Save» button.
Navigate to the Profile page and click plus button to register a New Provisioning Profile.
Select the «iOS App Development» option and click the «Continue» button.
Type the name of your Provisioning Profile and click Generate button. The name can be for example your app name.
Your Provisioning Profile is ready. Click the Download button. Then double click the following file to install your Provisioning Profile.
HOW TO SELECT YOUR PROVISIONING PROFILE IN XCODE
Open your Xcode project.
Select your Target.
Select «Signing & Capabilities».
Uncheck «Automatically manage signing».
In the Provisioning Profile field select the Profile that you downloaded and installed in previous steps.
HOW TO ADD ENTITLEMENTS FILE
Guide by mobiledev99
- Click on plus to add a capability.
- Pick «Keychain Sharing»
- Open .entitlements file. Hover your cursor to the «Entitlements File» field and click plus button.
- Change value key name to «com.apple.developer.usernotifications.critical-alerts», type to «Boolean» and value to «1».
- Then hover your cursor to the «Keychain Access Groups» field and click the minus button.
WHAT WE NEED TO ADD TO THE CODE
Request authorization
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound, .criticalAlert]) { (granted, error) in}
For local notifications
Change your UNMutableNotificationContent
sound to the next one:
let notificationContent = UNMutableNotificationContent()
notificationContent.sound = UNNotificationSound.defaultCritical
If you want to use your custom sound, you need to replace the code with this one:
let notificationContent = UNMutableNotificationContent()
notificationContent.sound = UNNotificationSound.criticalSoundNamed(UNNotificationSoundName(rawValue:"YourCustomSound.mp3"))
For push notifications(From Tapcode tutorial)
{
"aps":{
"alert": "This is a Critical Alert!",
"badge": 1,
"sound": {
"critical": 1,
"name": "your_custom_sound.aiff",
"volume": 1.0
}
}
}
There are several sources that can help you implement critical alerts for your iOS app. I found two that were very helpful:
https://www.tapcode.co/2018/10/17/how-to-implement-critical-alerts-in-ios-12/
Both describe what is necessary to request approval from Apple and how to update your project to include critical alerts notifications – with one exception. The articles explain how to update your entitlements file to add the critical alerts key. But what do you do when your app currently does not have an entitlements file? After a bit of research, I arrived at the following steps that can be used as a guide for dealing with this dilemma. When you get to the part of the articles that tackle updating the entitlements file please proceed as follows:
- Under the Capabilities tab, click on + (plus) to add a capability (I picked Keychain Sharing). This will cause Xcode to generate a .entitlements file (major hurdle overcome).
- Click on that file in the Project Navigator to open it. You will see the key for the capability that you have chosen (Keychain Sharing) if the disclosure triangle next to Entitlements File is open. If the disclosure triangle is not open, please click it to open it.
- Click on + (plus) next to Entitlements File to add an entitlement. A dropdown with selections will appear but you will have to type the key for critical alerts notifications in order to enter it manually.
- Enter the key for critical alerts notifications that is in the articles: com.apple.developer.usernotifications.critical-alerts
- Change the type to Boolean and select YES as the value.
- Click on – (dash) next to the capability that you used to create the entitlements file in step 1 (Keychain Sharing) to delete it. You now have an entitlements file with the critical alerts key that you need.
- Build and run your app and you should see the prompt that asks the user to allow critical alerts.
Mission accomplished!
Terry Chaisson
© 2022 - 2024 — McMap. All rights reserved.