UIUserNotificationSettings.types: UIUserNotificationType // A bitmask of the notification types that your app is allowed to use
UIUserNotificationSettings.categories: Set // The app’s registered groups of actions
A dictionary that contains remote notification info, potentially including a badge number for the app icon, alert sound, alert message, a notification identifier, and custom data.
Checking if your app is already registered for Push Notification
Generating a .pem certificate from your .cer file, to pass on to the server developer
Save aps.cer to a folder
Open "Keychain access" and export the key that is under that certificate to a .p12 file (call it key.p12). To do that right click on it and choose Export. Save it to the same folder as step 1. On export you will be prompted for a password. Make something up and memorize it.
cd to that folder in Terminal and execute the following commands:
Convert .cer to a .pem certificate
Convert your key to .pem format. To open the key, enter the password you exported it with from the keychain, in step 2. Then, enter another password that will protect the exported file. You will be prompted to enter it twice for confirmation.
Merge the files into one final file
The final_cert.pem is the final result. Pass it on to server developers with the password from step 5, so that they will be able to use the protected certificate.
Handling Push Notification
Once user clicks on a push notification, the following callback function will be called. You can parse the JSON to get any specific info sent from backend that will helps you in deep linking:
Registering App ID for use with Push Notifications
Things you need
A paid Apple Developer Program Membership
A valid App ID and identifier for you app (like com.example.MyApp) which is not used before anywhere
An iOS Device to test (as Push Notifications don't work on Simulator)
Enabling the APNs access for App ID in Apple Developer Center
1- Log in to developer.apple.com Member Center (the Account link on the home page)
2- Go to "Certificates"
3- Select "App ID" from left panel
4- Click on "+" on top right
5- Add App ID with Push Notifications option checked
6- Click on created App ID and select Edit
7- Click Configure in Push Notifications panel
8- Open Keychain Access app in your Mac
9- From Keychain Access menu, click Certificate Assistant -> Request a Certificate from a Certificate Authority
10- Enter your mail in the first text field
11- Enter your name in the second text field
12- Leave CA Email Address empty
13- Select Saved to disk rather than Emailed to the CA
14- Click Continue and upload the generated file
15- Download the generated file by Apple and open it while Keychain Access is open
Enabling the APNs access in Xcode
1- Select your project
2- Open Capabilities tab
3- Find Push Notifications and turn it on
4-Find Background Modes and turn it on and check Remote Notifications
Registering device for Push Notifications
To register your device for push notifications, add the following code to your AppDelegate file in didFinishLaunchingWithOptions method:
The above code will try to communicate with APNs server to get device token (prerequiesites are you have APNs enabled in your iOS provisioning profile).
Once it establishes reliable connection with APNs server, the server provides you a device token.
After adding the code above, add these methods to the AppDelegate class:
The above methods are called according to registration success or failure scenario.
Success scenario calls:
Failure scenario calls:
If none of the above methods are getting called, your device is not able to create reliable connection with APNs server, which might be because of internet access problems.
Registering for (Non Interactive) Push Notification
The logic of registering for push notification is recommended to be added in AppDelegate.swift as the callback functions (success, failure) will be called their. To register just do the following:
Then the callback function didRegisterUserNotificationSettings will be called and in that case you just trigger the register like this:
And in that case and system alert will be shown asking for persmission to receive push notification. One of the following callback functions will be called:
In very rare cases, neither success or failure callback functions are called. This happens when you have internet connection problems or the APNS Sandbox is down. The system do an API call to APNS to do some verification, failing to do so will lead to none of the two callbacks functions will be called. Visit Apple system status to make sure its fine.
Setting the application icon badge number
Use the following piece of code to set the badge number from within your application (suppose someNumber has been declared before):
In order to remove the badge completely, just set someNumber = 0.
Testing push notifications
It is always a good practice to test how push notifications work even before you have your server side ready for them, just to make sure that everything is set up correctly on your side. It is quite easy to send yourself a push notification using a following PHP script.
Save the script as a file (send_push.php for example) in the same folder as your certificate (development or production)
Edit it to put your device token, password from the certificate
Choose the correct path for opening a connection, dev_path or prod_path (this is where 'Open a connection to the APNS server' happens in the script)
cd to the folder in Terminal and run command 'php send_push'
Receive the notification on your device
Unregistering From Push Notifications
To unregister from Remote Notifications programatically you can use
this is similar to going into the setting of your phone and manually switching off Notifications for the application.
NOTE: There may be rare cases where you would need this(eg: when your app no longer supports push notifications)
If you just want to allow the user to temporarily disable Notifications. You should implement a method to remove device token in the database on your server.
else if you only disable Notification locally on your device your server will still send messages.
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0