PUSH Notification

 APNS

APNs Overview. Apple Push Notification service (APNs) is the centerpiece of the remote notifications feature. It is a robust, secure, and highly efficient service for app developers to propagate information to iOS (and, indirectly, watchOS), tvOS, and macOS devices

Device Token

APNS generates a unique token/id for a apple device. This can be used while sending notification.

Push Payload Size

When using the HTTP/2 based APNS provider API, the maximum size for your JSON dictionary is 4KB. For legacy APIs, the payload size is smaller.

  • For regular remote notifications, the maximum size is 4KB (4096 bytes)
  • For Voice over Internet Protocol (VoIP) notifications, the maximum size is 5KB (5120 bytes)


Payload Sample: 
{
  "aps": {
    "alert": {
      "title": "New Podcast Available",
      "subtitle": "Antonio Leiva – Clean Architecture",
      "body": "This episode we talk about Clean Architecture with Antonio Leiva."
    },
    "mutable-content": 1
  },
  "podcast-image": "https://koenig-media.raywenderlich.com/uploads/2016/11/Logo-250x250.png",
  "podcast-guest": "Antonio Leiva"
}



What is push notification? How it works?

Assuming your app is push enabled and certificate has been shared to provider.







 Step 1: Register for APNS 


func registerForRemoteNotification() {

        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()

    }




 Step 2: Get device token (Delegate method will be called) 


func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

        

        //send this device token to server

        

        // 1. Convert device token to string

       let tokenParts = deviceToken.map { data -> String in

        return String(format: "%02.2hhx", data)

       }


        let token = tokenParts.joined()


        // 2. Print device token to use for PNs payloads

        print("Device Token: \(token)")

    }



//Called if unable to register for APNS.


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {

        print(error)

    }




Step 3: On notification received (Delegate method will be called)


func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {


            println("Recived: \(userInfo)")

           //Parsing userinfo:

           var temp : NSDictionary = userInfo

           if let info = userInfo["aps"asDictionary<StringAnyObject>

                    {

                        var alertMsg = info["alert"asString

                        var alert: UIAlertView!

                        alert = UIAlertView(title: "", message: alertMsg, delegate: nil, cancelButtonTitle: "OK")

                        alert.show()

                    }

        }




Step 4: Check if permission given


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")

            }

        }















Comments

Popular posts from this blog

NotificationCenter