Despite the fact that the answer is given for processing push notifications, I still believe that I will immediately tell you about the integrated finished case:
To register an application for APNS, (include the following code in the didFinishLaunchingWithOptions method inside AppDelegate.swift)
IOS 9
var settings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications()
After iOS 10
UserNotifications framework introduced:
Import the UserNotifications structure and add UNUserNotificationCenterDelegate to AppDelegate.swift.
Register APNS Application
let center = UNUserNotificationCenter.current() center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in // Enable or disable features based on authorization. } application.registerForRemoteNotifications()
This will call the following delegate method
func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { //send this device token to server } //Called if unable to register for APNS. func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { println(error) }
Upon receipt of the notification, the following delegate will call:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { println("Recived: \(userInfo)") //Parsing userinfo: var temp : NSDictionary = userInfo if let info = userInfo["aps"] as? Dictionary<String, AnyObject> { var alertMsg = info["alert"] as! String var alert: UIAlertView! alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK") alert.show() } }
To determine this permission, we can use:
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in switch setttings.soundSetting{ case .enabled: print("enabled sound") case .disabled: print("not allowed notifications") case .notSupported: print("something went wrong here") } }
So, the APNS checklist:
- AppId creation allowed with push notification
- Create SSL certificate with valid certificate and application ID
- Create a Provisioning profile with the same certificate and be sure to add the device in case of an isolated environment (preparation for development). Note. It will be good if you create a Provisioning profile after an SSL certificate.
With code:
- Register your push notification application
- Handle the didRegisterForRemoteNotificationsWithDeviceToken method
- Set Goals> Features> Backgrounds> Remote Notification
- Handle didReceiveRemoteNotification