Push notification implementation in Swift

Posted By : Manu Gupta | 30-Jun-2015

Creating Certificate from Keychain Access

 

  1. Open Keychain access -> Certificate Assisstant -> Request a Certificate from a Certificate Authority.

  2. Fill your email id and certificate name as shown in picture and after that save the certificate file.

  3. Go to keys tab and find your certificate and export your certificate as shown in image.

  4. Next save the private key as pushnotification.p12

 

Making the App ID and SSL Certificate

  1. Open developer account with user credentials.
  2. Create App ID.
  3. Download Certificate file with aps_development.cer name.
  4. Create Provisional profile with package name.
  5. Download Provisional Profile.

Now if you have a server you can just send the export p12 file to the server. and provide device token to server. If you dont have server then generate a PEM file.

Converting files to PEM File

So now we have three files:

  1. The CSR(Certificate Signing Request)
  2. The private key as a p12 file (pushnotification.p12)
  3. The SSL certificate, aps_development.cer
  • We need to save these file as they can be used afterwards. After that our first step is to convert .cer file into a .pem file and to do so open the terminal and run the following commands.
    	openssl x509 -in aps_development.cer -inform der -out pushnotificationCert.pem
                 	
  • Now we need to convert the private key's .p12 file into a .pem file, execute the below commamds.
    	openssl pkcs12 -nocerts -out pushnotificationKey.pem -in pushnotification.p12
        Enter Import Password: 
        MAC verified OK
        Enter PEM pass phrase: 
        Verifying - Enter PEM pass phrase:
                 
  • Now we need to convert both cert.pem and key.pem file into one .pem file.
       cat pushnotificationCert.pem pushnotificationKey.pem > ck.pem
                 
  • If you want to check weather your certificate work or now execute the following line:
       telnet gateway.sandbox.push.apple.com 2195
       Trying 17.172.232.226...
       Connected to gateway.sandbox.push-apple.com.akadns.net.
       Escape character is '^]'.
                 
  • This will create a reguiler connection to APNS. Now if the above response comes then terminate the command and execute another command.
       openssl s_client -connect gateway.sandbox.push.apple.com:2195 
       -cert pushnotificationCert.pem -key pushnotificationKey.pem
       Enter pass phrase for pushnotificationKey.pem:
                 

    THANKS

  • Enter this code in your App Delegate Method
     if application.respondsToSelector("registerUserNotificationSettings:") {
                
                let types:UIUserNotificationType = (.Alert | .Badge | .Sound)
                let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
                
                application.registerUserNotificationSettings(settings)
                application.registerForRemoteNotifications()
                
            } else {
                // Register for Push Notifications before iOS 8
                application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
            }
    
     
  • Add these methods to handle the push notification
    
        func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
            var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
            var deviceTokenString: String = ( deviceToken.description as NSString )
                .stringByTrimmingCharactersInSet( characterSet )
                .stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
            NSUserDefaults.standardUserDefaults().setObject(deviceTokenString, forKey: "deviceToken")
        }
        
        //Called if unable to register for APNS.
        func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
            println(error)
        }
     
  • Now you are up for push notification. Thanks

About Author

Author Image
Manu Gupta

Manu is a Native iOS (Swift) and Titanium lead developer . Manu has good experience working with Swift and Alloy framework as well. He loves watching movies and travelling.

Request for Proposal

Name is required

Comment is required

Sending message..