Push Notification iOS

NOTE :

Push notification work on bases of device token so you need real IOS device to test notifications it can’t be tested on Stimulator.

Push notification is a message alert that is generated on user devices when publisher send any message or trigger any action. To seek user attention notification is used so that user can be alert about message and can take action on immediately.

APNS LIFE CYCLE OR WORKING

  1. App register for push notification.
  2. OS asks APNS for the device token.
  3. App receive device token.
  4. App send token to server.
  5. Server sends notification to your device on token base.

Creates certificates for Push notifications

  1. Register app bundle identifier in apple developer account.
  2. Create SSL certificate for the same app Id.
  3. Create provisioning profile for push notification with adding devices for testing push notifications. openssl pkcs12 -in apns-dev-cert.p12 -out apns-dev-cert.pem -nodes -clcerts
  4. 5. You can test you pemp file from:-> https://github.com/noodlewerk/NWPusher

From Code

  1. Register for push notification.
  2. Handle token method i.e didRegisterForRemoteNotificationsWithDeviceToken .
  3. From Xcode target set: targets> Capability> background modes> Remote Notification
  4. Handle notification from didRecieveRemoteNotification method.
  1. Register Application for APNS
  2.     
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        // If granted comes true you can enabled features based on authorization.
        guard granted else { return }
        application.registerForRemoteNotifications()
        }
        
    
  3. Will call following delegate method to generate device token.
  4.     
        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) { print(error)}
    
        
    
  5. On Receiving notification delegate method will be call:
  6.     
        func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        println("Recived: \(userInfo)")
        //Parsing userinfo:
        var temp : NSDictionary = userInfo
        if let info = userInfo["aps"] as? Dictionary     {
        var alertMsg = info["alert"] as! String
        var alert: UIAlertView!
        alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        }
        }
    
        
    
  7. For permission request we use:
  8.     
        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")
        }
        }
        
    

Notification Payload Format:

    
    {
    "aps" : {
    "alert" : "It's a notification with custom payload!",
    "badge" : 1,
    "content-available" : 0         
    },
    "data" :{
    "title" : "Game Request",
    "body" : "Bob wants to play poker",
    "action-loc-key" : "PLAY"
    },

    }

    
I Hope you enjoy this blog

Thank You

Want a Team that Delivers Result ? Connect now with us.

Our Offices

INDIA

F-429, Phase 8B, Industrial Area, SAS Nagar, Punjab 160059

+91 82198-18163

USA

13506 Summerport Village Pky Suite 355 Windermere, FL 34786

+1 (321) 900-0079

CANADA

15 Meltwater Cres, Brampton L6P3V8

+1 (647) 892-6147