Register push notification Swift 3 + iOS 10

Ashvin A picture Ashvin A · Aug 13, 2016 · Viewed 9.7k times · Source

I'm trying to implement rich push notification but having issue with register push notification.

Anybody help me?

Answer

Ashvin A picture Ashvin A · Aug 13, 2016

I checked the apple doc and found the one way, some Class is depericated in iOS 10 which we using till iOS 9.x

Steps are there:

  1. Add framework UserNotifications
  2. Add one keys in info plist (I did because i'm using background fetch), check screenshot
  3. Use below code and send the token to your server

To register remote notification

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in

        // Enable or disable features based on authorization.
        if granted == true
        {
            print("Allow")
            UIApplication.shared.registerForRemoteNotifications()
        }
        else
        {
            print("Don't Allow")
        }
    }

Get Token

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print(deviceToken)
}

enter image description here