Registering for Push Notifications in Xcode 8/Swift 3.0?
Asked Answered
M

11

124

I'm trying to get my app working in Xcode 8.0, and am running into an error. I know this code worked fine in previous versions of swift, but I'm assuming the code for this is changed in the new version. Here's the code I'm trying to run:

let settings = UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil)     
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.shared().registerForRemoteNotifications()

The error that I'm getting is "Argument labels '(forTypes:, categories:)' do not match any available overloads"

Is there a different command that I could try to get this working?

Murky answered 22/6, 2016 at 0:1 Comment(1)
I wrote a guide on how to do just that: eladnava.com/…Scheme
M
313

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")
            }
        }
Metameric answered 22/6, 2016 at 11:41 Comment(11)
I get error in swift 2.3: UNUserNotificationCenter has no member currentOptimize
Hay can you provide the save in objective cUnmanned
@Optimize You're not seeing current() because it's only working in Swift 3.Ranjiv
Don't forget to link UserNotifications.framework to your target, and to import it in your AppDelegate. ;)Cookgeneral
Thanks at first and please excuse the following question, that results just from a lack of experience and knowledge: What is the purpose of the deviceToken, what for is it used?Titlark
@Titlark The device token represents the identity of the device receiving the notification. APNs uses device tokens to identify each unique app and device combination.See more hereNahshon
In the closure, you'll need to perform UI related tasks on the Main Thread ... DispatchQueue.main.async { ... update a label ... }Xeres
@ChrisAllinson the callback is always in main thread after the operation completesNahshon
@ThatlazyiOSGuy웃 Can u tell me step by step procedure if user will register in website app will get a notificationsAlina
application.registerForRemoteNotifications() goes first, then request permission. The first step just may get several call back from iOS system in AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken if you turn the notification switch on and off several times.Inglis
how to add/triger multiple notifications?Freehanded
C
48
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    if #available(iOS 10, *) {

        //Notifications get posted to the function (delegate):  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)"


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

            guard error == nil else {
                //Display Error.. Handle Error.. etc..
                return
            }

            if granted {
                //Do stuff here..

                //Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                }
            }
            else {
                //Handle user denying permissions..
            }
        }

        //Register for remote notifications.. If permission above is NOT granted, all notifications are delivered silently to AppDelegate.
        application.registerForRemoteNotifications()
    }
    else {
        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
    }

    return true
}
Catch answered 22/6, 2016 at 0:40 Comment(14)
What's the added benefit of this new framework? What I see here is a using 'completionHandler over delegate' approach and then the decision making is given to you right away: error, granted, or notGranted .... In 6< iOS <10 you had to do ‍application.isRegisteredForRemoteNotifications() to see if it's granted, and use another delegate method in case you had an error. Right? Anything else?Emissary
why is your's different from the accepted answer? He has a application.registerForRemoteNotifications() after his center.requestAuthorizationEmissary
@Honey; That is added if you want "Remote" notifications enabled. When I wrote my answer, no other answer existed and @OP did not specify whether they wanted remote or local or iOS 10 support so I added as much as I could. Note: You shouldn't register for RemoteNotifications until the user has granted access (otherwise all remote notifications are delivered silently [unless that's what you want ] -- no popups). Also, the advantage of the new API is that it supports attachments. In other words, you can add GIF's and other Images, Videos, etc.. to your notifications.Catch
To add attachments, you do: notification.content.attachments = [UNNotificationAttachment(identifier: "png_identifier", url:imageURL , options: nil)] or UNNotificationAttachment(identifier: "audio_identifier", url:imageURL , options: [UNNotificationAttachmentOptionsTypeHintKey:kUTTypeMP3]), etc.. for example.Catch
Oh ok... interesting.... You mean the payload will have a URL of the gif? or it's being sent as Data?Emissary
@Honey the notification will actually display as a gif when it is received and opened. If it is a video, it will show a thumbnail just like an image. The URL will be downloaded by the receiving device. If it is local, it will be sent as data and the receiving device will display it correctly. I believe videos will only be sent as a URL but with a preview (like in iMessage). I haven't tried all of them yet; just images and gifs.Catch
In the closure, you'll need to perform UI related tasks on the Main Thread ... DispatchQueue.main.async { ... do stuff here ... }Xeres
I want to display Push Notifications in Foreground State for iOS 8.0,i m receiving notifications in background state @ChrisAllinsonAlina
@DilipTiwari last I checked you can "hear" that a push is received, but you have to manually handle that case in the didReceiveRemoteNotification AppDelegate method (and manually display something to the user) ... some further SO reading: #14872588Xeres
The error in requestAuthorization(options:completionHandler:) is always nil whenever I refuse to receive notification or do not provide a aps-certificate. How to get a real error?Inglis
Your answer is not very clear. application.registerForRemoteNotifications() goes first, then request permission. The first step just may get several call back from iOS system in AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken if you turn the notification switch on and off several times.Inglis
how to add/triger multiple notifications?Freehanded
@Inglis I don't believe the order matters to iOS. It may matter to your app, though. If you don't seek and receive permission, iOS will silently notify your app. So it's generally best to ask user for permission BEFORE registering (and then only registering if granted the permission) unless you want the silent notifications.Profane
Benefit of this solution when use not in AppDelegate to do same thing in codeSon
R
29
import UserNotifications  

Next, go to the project editor for your target, and in the General tab, look for the Linked Frameworks and Libraries section.

Click + and select UserNotifications.framework:

// iOS 12 support
if #available(iOS 12, *) {  
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound, .provisional, .providesAppNotificationSettings, .criticalAlert]){ (granted, error) in }
    application.registerForRemoteNotifications()
}

// iOS 10 support
if #available(iOS 10, *) {  
    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]){ (granted, error) in }
    application.registerForRemoteNotifications()
}
// iOS 9 support
else if #available(iOS 9, *) {  
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    UIApplication.shared.registerForRemoteNotifications()
}
// iOS 8 support
else if #available(iOS 8, *) {  
    UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
    UIApplication.shared.registerForRemoteNotifications()
}
// iOS 7 support
else {  
    application.registerForRemoteNotifications(matching: [.badge, .sound, .alert])
}

Use Notification delegate methods

// Called when APNs has assigned the device a unique token
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {  
    // Convert token to string
    let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("APNs device token: \(deviceTokenString)")
}

// Called when APNs failed to register the device for push notifications
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {  
    // Print the error to console (you should alert the user that registration failed)
    print("APNs registration failed: \(error)")
}

For receiving push notification

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

Setting up push notifications is enabling the feature within Xcode 8 for your app. Simply go to the project editor for your target and then click on the Capabilities tab. Look for Push Notifications and toggle its value to ON.

Check below link for more Notification delegate methods

Handling Local and Remote Notifications UIApplicationDelegate - Handling Local and Remote Notifications

https://developer.apple.com/reference/uikit/uiapplicationdelegate

Rete answered 9/3, 2017 at 5:29 Comment(0)
E
20

I had issues with the answers here in converting the deviceToken Data object to a string to send to my server with the current beta of Xcode 8. Especially the one that was using deviceToken.description as in 8.0b6 that would return "32 Bytes" which isn't very useful :)

This is what worked for me...

Create an extension on Data to implement a "hexString" method:

extension Data {
    func hexString() -> String {
        return self.reduce("") { string, byte in
            string + String(format: "%02X", byte)
        }
    }
}

And then use that when you receive the callback from registering for remote notifications:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let deviceTokenString = deviceToken.hexString()
    // Send to your server here...
}
Erastes answered 27/8, 2016 at 9:4 Comment(3)
I also had the "32bytes" problem. Great solution, you can do the conversion in-line without creating an extension though. Like this: let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})Noneffective
Absurd that there is no solution coming from the API itselfFirework
Yeah it has always been pretty weird that API.. surprised they didn't fix it when doing the new notifications framework in iOS10Erastes
N
18

In iOS10 instead of your code, you should request an authorization for notification with the following: (Don't forget to add the UserNotifications Framework)

if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted: Bool, error: NSError?) in
            // Do something here
        }
    }

Also, the correct code for you is (use in the else of the previous condition, for example):

let setting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
UIApplication.shared().registerUserNotificationSettings(setting)
UIApplication.shared().registerForRemoteNotifications()

Finally, make sure Push Notification is activated under target-> Capabilities -> Push notification. (set it on On)

Nuthouse answered 22/6, 2016 at 0:36 Comment(6)
see : Page 73 of the Apple Doc hereNuthouse
Thank you so much for the reply! Using the code, however, it is saying "Use of unresolved identifier 'UNUserNotificationCenter'"Murky
And thank you so much for the documentation, blablabla! I didn't see that on their site, I'm glad it exists. :DMurky
I'm so excited, I've been wanting to add push notifications to my app (been learning Xcode as a hobby for the last year, but there are still so many things I need to know), and with the new notifications, I figure now is the time. I got notifications working with my dev iPhone, now I just have to figure out how to get the tokens for phones running the production version and pass them to the php script I'm using.Murky
What's the added benefit of this new framework? What I see here is a using 'completionHandler over delegate' approach and then the decision making is given to you right away: error, granted, or notGranted .... In 6< iOS <10 you had to do ‍application.isRegisteredForRemoteNotifications() to see if it's granted, and use another delegate method in case you had an error. Right? Anything else?Emissary
I want to display Push Notifications in Foreground State for iOS 8.0,i m receiving notifications in background state @NuthouseAlina
B
8

Well this work for me. First in AppDelegate

import UserNotifications

Then:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        registerForRemoteNotification()
        return true
    }

    func registerForRemoteNotification() {
        if #available(iOS 10.0, *) {
            let center  = UNUserNotificationCenter.current()
            center.delegate = self
            center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
                if error == nil{
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
        }
        else {
            UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
            UIApplication.shared.registerForRemoteNotifications()
        }
    }

To get devicetoken:

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

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

}
Bobolink answered 17/10, 2016 at 13:28 Comment(0)
B
6

Heads up, you should be using the main thread for this action.

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        if granted {
            DispatchQueue.main.async(execute: {
                UIApplication.shared.registerForRemoteNotifications()
            })
        }
    }
Breaking answered 17/10, 2017 at 18:38 Comment(0)
I
2

First, listen to user notification status, i.e., registerForRemoteNotifications() to get APNs device token;
Second, request authorization. When being authorized by the user, deviceToken will be sent to the listener, the AppDelegate;
Third, report the device token to your server.

extension AppDelegate {
   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       // 1. listen(监听) to deviceToken
       UIApplication.shared.registerForRemoteNotifications()
       // 2. request device token
       requestAuthorization()
   }

   func requestAuthorization() {
        if #available(iOS 10, *) {
            let uc = UNUserNotificationCenter.current()
            uc.delegate = UIApplication.shared.delegate as? AppDelegate
            uc.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
                if let error = error { // 无论是拒绝推送,还是不提供 aps-certificate,此 error 始终为 nil
                    print("UNUserNotificationCenter 注册通知失败, \(error)")
                }
                DispatchQueue.main.async {
                    onAuthorization(granted: granted)
                }
            }
        } else {
            let app = UIApplication.shared
            app.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil))
        }
    }

    // 在 app.registerUserNotificationSettings() 之后收到用户接受或拒绝及默拒后,此委托方法被调用
    func application(_ app: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
        // 已申请推送权限,所作的检测才有效
        // a 征询推送许可时,用户把app切到后台,就等价于默拒了推送
        // b 在系统设置里打开推送,但关掉所有形式的提醒,等价于拒绝推送,得不token,也收不推送
        // c 关掉badge, alert和sound 时,notificationSettings.types.rawValue 等于 0 和 app.isRegisteredForRemoteNotifications 成立,但能得到token,也能收到推送(锁屏和通知中心也能看到推送),这说明types涵盖并不全面
        // 对于模拟器来说,由于不能接收推送,所以 isRegisteredForRemoteNotifications 始终为 false
       onAuthorization(granted: app.isRegisteredForRemoteNotifications)
    }

    static func onAuthorization(granted: Bool) {
        guard granted else { return }
        // do something
    }
}

extension AppDelegate {
    func application(_ app: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        //
    }
    
    // 模拟器得不到 token,没配置 aps-certificate 的项目也得不到 token,网络原因也可能导致得不到 token
    func application(_ app: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        //
    }
}
Inglis answered 10/11, 2017 at 14:12 Comment(3)
how to add multiple notification?Freehanded
@ArgaPK, To send push notifications is what your server platform does.Inglis
Thanks for your contribution! Please note that content on [main] is expected to be in english: "Comments in code blocks must be written in English". Would you please translate your code comments to english?Jaquenette
P
0

The answer from ast1 is very simple and useful. It works for me, thank you so much. I just want to poin it out here, so people who need this answer can find it easily. So, here is my code from registering local and remote (push) notification.

    //1. In Appdelegate: didFinishLaunchingWithOptions add these line of codes
    let mynotif = UNUserNotificationCenter.current()
    mynotif.requestAuthorization(options: [.alert, .sound, .badge]) {(granted, error) in }//register and ask user's permission for local notification

    //2. Add these functions at the bottom of your AppDelegate before the last "}"
    func application(_ application: UIApplication, didRegister notificationSettings: UNNotificationSettings) {
        application.registerForRemoteNotifications()//register for push notif after users granted their permission for showing notification
}
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
    print("Device Token: \(tokenString)")//print device token in debugger console
}
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    print("Failed to register: \(error)")//print error in debugger console
}
Palmette answered 19/9, 2016 at 8:19 Comment(0)
E
0

Simply do the following in didFinishWithLaunching::

if #available(iOS 10.0, *) {

    let center = UNUserNotificationCenter.current()

    center.delegate = self
    center.requestAuthorization(options: []) { _, _ in
        application.registerForRemoteNotifications()
    }
}

Remember about import statement:

import UserNotifications
Extender answered 10/5, 2017 at 11:51 Comment(1)
I believe this should be the accepted answer. It seems correct to call registerForRemoteNotifications() in the completion handler of requestAuthorization(). You may even want to surround registerForRemoteNotifications() with an if granted statement: center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in if granted { UIApplication.shared.registerForRemoteNotifications() } }Deidradeidre
P
-1

Take a look at this commented code:

import Foundation
import UserNotifications
import ObjectMapper

class AppDelegate{

    let center = UNUserNotificationCenter.current()
}

extension AppDelegate {

    struct Keys {
        static let deviceToken = "deviceToken"
    }

    // MARK: - UIApplicationDelegate Methods
    func application(_: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

        if let tokenData: String = String(data: deviceToken, encoding: String.Encoding.utf8) {
            debugPrint("Device Push Token \(tokenData)")
        }

        // Prepare the Device Token for Registration (remove spaces and < >)
        setDeviceToken(deviceToken)
    }

    func application(_: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        debugPrint(error.localizedDescription)
    }

    // MARK: - Private Methods
    /**
     Register remote notification to send notifications
     */
    func registerRemoteNotification() {

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

            // Enable or disable features based on authorization.
            if granted  == true {

                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                debugPrint("User denied the permissions")
            }
        }
    }

    /**
     Deregister remote notification
     */
    func deregisterRemoteNotification() {
        UIApplication.shared.unregisterForRemoteNotifications()
    }

    func setDeviceToken(_ token: Data) {
        let token = token.map { String(format: "%02.2hhx", arguments: [$0]) }.joined()
        UserDefaults.setObject(token as AnyObject?, forKey: “deviceToken”)
    }

    class func deviceToken() -> String {
        let deviceToken: String? = UserDefaults.objectForKey(“deviceToken”) as? String

        if isObjectInitialized(deviceToken as AnyObject?) {
            return deviceToken!
        }

        return "123"
    }

    func isObjectInitialized(_ value: AnyObject?) -> Bool {
        guard let _ = value else {
                return false
         }
            return true
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping(UNNotificationPresentationOptions) -> Swift.Void) {

        ("\(notification.request.content.userInfo) Identifier: \(notification.request.identifier)")

        completionHandler([.alert, .badge, .sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) {

        debugPrint("\(response.notification.request.content.userInfo) Identifier: \(response.notification.request.identifier)")

    }
}

Let me know if there is any problem!

Platinotype answered 14/5, 2018 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.