Answers for "what you in swift to do a push notifications"

1

register for remote notifications swift

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

// 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")
    }
}
Posted by: Guest on January-31-2020

Code answers related to "what you in swift to do a push notifications"

Code answers related to "Swift"

Browse Popular Code Answers by Language