Registering For Push Notifications in Xcode 8/Swift 3.0

Registering for Push Notifications in Xcode 8/Swift 3.0?

Import the UserNotifications framework and add the UNUserNotificationCenterDelegate in AppDelegate.swift

Request user permission

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
application.registerForRemoteNotifications()
return true
}

Getting device token

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

let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
print(deviceTokenString)
}

In case of error

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

print("i am not available in simulator \(error)")
}

In case if you need to know the permissions granted

UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in

switch settings.soundSetting{
case .enabled:

print("enabled sound setting")

case .disabled:

print("setting has been disabled")

case .notSupported:
print("something vital went wrong here")
}
}

Unable to Register for Push (Xcode 8)

There is an settings in Xcode 8 that you have to turn it on:

Sample Image

Credit goes to DipakSonara's answer here

Register push notification Swift 3 + iOS 10

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

Sample Image

Swift 3 - Firebase Push Notification, How can I do?

The AppDelegate method names have changed a little and UserNotifications framework has been introduced. You must use this framework for notifications in iOS 10 and above as the other methods are being deprecated.

import UserNotifications

...

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

...

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}

application.registerForRemoteNotifications()

return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> ()) {

print("Message ID \(userInfo["gcm.message_id"]!)")
print(userInfo)
}

...
}

Registering for Push Notifications in Xcode 8/Swift 3.0?



Related Topics



Leave a reply



Submit