4

I'm running into a strange problem. When I request for remote notifications authorization inside the didFinishLaunchingWithOptions using the below code, I'm able to get a push remote notification from my server (Firebase Messaging) but when I try to request for authorization at later time NOT INSIDE didFinishLaunchingWithOptions I'm not able to get any notification.

I don't want to ask for authorisations at the launch time. I want to ask for authorization after the user have logged in for example.

Apple documentation says:

Always call this method before scheduling any local notifications and before registering with the Apple Push Notification Service. Typically, you call this method at launch time when configuring your app's notification support. However, you may call it at another time in your app's life cycle, providing that you call it before performing any other notification-related tasks.

 // For iOS 10 display notification (sent via APNS)
 UNUserNotificationCenter.current().delegate = self

 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

 UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })

 UIApplication.shared.registerForRemoteNotifications()

Any suggestion/solution to this problem?

App Info: Deployment Target: iOS 10.3 iOS Devices: iPhone 6 with iOS 10 / iPhone 7 with iOS 11

Jad
  • 2,139
  • 1
  • 16
  • 28
  • where's the other point you call it at? – TNguyen Sep 26 '17 at 13:21
  • what do you mean?! – Jad Sep 26 '17 at 13:22
  • where else do you request for authorization again? Also just curious why do you need to register it again at a later time? Or want to register it at a later time – TNguyen Sep 26 '17 at 13:23
  • Also, maybe try seeing if `application(_:didFailToRegisterForRemoteNotificationsWithError:)` can help you out too – TNguyen Sep 26 '17 at 13:26
  • @TPN1994 I'm requesting from inside a user action handler. I'm not requesting again! I just don't want to ask for authorisations at the launch time. didRegisterForRemoteNotificationsWithDeviceToken is successfully called – Jad Sep 26 '17 at 13:28
  • what does the below line print NSLog(@"APNs token retrieved: %@", deviceToken) , pls share some more code what and where you are trying – Ajay Beniwal Sep 26 '17 at 13:37
  • FWIW you know you can call `UIApplication.shared.registerForRemoteNotifications()` anytime you like right? And the user would NEVER know! – mfaani Sep 26 '17 at 13:40

1 Answers1

2

So Here's how I solved it:

In my didFinishLaunchingWithOptions I register for remote notification like so:

UNUserNotificationCenter.current().delegate = self
UIApplication.shared.registerForRemoteNotifications()

and then at later time, for example after the user have logged in, I ask for the authorization by calling the method below;

 func reqeust()  {
        // For iOS 10 display notification (sent via APNS)
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]

        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in



        })

    }
Jad
  • 2,139
  • 1
  • 16
  • 28
  • So you actually call `requestAuthorization` after `registerForRemoteNotifications`, despite what the documentation says. Did you get the tokens this way? – NeoWang Oct 15 '17 at 18:04
  • I'm using Firebase remote notifications, so I got it anyway even if the user didn't requestAuthorization. If you are not using Firebase then I think once you requestAuthorization you will get it. – Jad Oct 16 '17 at 06:37