Registering for Push Notifications in Xcode 8/Swift 3.0?
Import the
UserNotifications
framework and add theUNUserNotificationCenterDelegate
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:
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:
- Add framework
UserNotifications
- Add one keys in info plist (I did because i'm using background fetch), check screenshot
- 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)
}
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
How to Set the Cookies to Be Used by a Wkwebview
Number of Days Between Two Nsdates
Status Bar and Navigation Bar Appear Over My View'S Bounds in iOS 7
Creating a Segue Programmatically
How to Hide Uinavigationbar 1Px Bottom Line
Generate .Pem File Used to Set Up Apple Push Notifications
What Is the Ibeacon Bluetooth Profile
Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Values
How to Change the Name of an iOS App
Uiscrollview Scrollable Content Size Ambiguity
Cfnetwork Sslhandshake Failed iOS 9
How to Localise a String Inside the iOS Info.Plist File
Expand/Collapse Section in Uitableview in Ios
Iphone: Detecting User Inactivity/Idle Time Since Last Screen Touch
How to Load Custom Uitableviewcells from Xib Files
How to Create a Custom iOS View Class and Instantiate Multiple Copies of It (In Ib)