0

My app has push notifications enabled, and up until today they were working properly. From setting breakpoints I've realized that the issue is that didRegisterForRemoteNotificationsWithDeviceToken is no longer being called. This is confusing to me as my notifications code hasn't change since when it was working (yesterday). Here is what my AppDelegate looks like:

 import UserNotifications

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

    registerForPushNotifications()

    return true
}

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

      print("Permission granted: \(granted)")
      guard granted else { return }
      self?.getNotificationSettings()
    }
}

func getNotificationSettings() {
    UNUserNotificationCenter.current().getNotificationSettings { settings in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
        }
    }
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
    let token = tokenParts.joined()
    print("Device Token: \(token)")
    global_device_token = token
}

The call to UIApplication.shared.registerForRemoteNotifications() in getNotificationSettings does get run however.

drsanger73
  • 41
  • 3
  • Is it possible that your APNS certificate expired? If you didn't change your code at all, that might be an option. – Claudio Redi Jun 27 '20 at 02:20
  • I would remove the `guard settings.authorizationStatus == ...` check - You can always register for remote notifications. If you don't have permission to show them then nothing will be shown. – Paulw11 Jun 27 '20 at 02:21
  • checkout this answer: https://stackoverflow.com/a/59156989/3278326 – Jarvis The Avenger Jun 27 '20 at 02:22
  • @drsanger73 Can you please specify which device / iOS version you are using? – Jarvis The Avenger Jun 27 '20 at 02:23
  • @ClaudioRedi the certificate hasn't expired yet – drsanger73 Jun 27 '20 at 02:28
  • @JarvisTheAvenger Disconnecting from Wifi didn't help. I'm testing this on an iPhone 8 running iOS 13.4, and an iPhone 8 Plus running some version of iOS 13 (unable to check) – drsanger73 Jun 27 '20 at 02:29
  • @drsanger73 Can you please try deleting an app and reinstalling it? – Jarvis The Avenger Jun 27 '20 at 02:30
  • @drsanger73 also check if your `didFailToRegisterForRemoteNotificationsWithError` method is getting called if not `didRegisterForRemoteNotificationsWithDeviceToken ` – Jarvis The Avenger Jun 27 '20 at 02:31
  • @JarvisTheAvenger I tried deleting and installing, still didn't work. However, restarting my phone did work, and `didRegisterForRemoteNotificationsWithDeviceToken` is being called now. I suppose the question is why wasn't it working in the first place? – drsanger73 Jun 27 '20 at 02:44
  • Scratch that, `didRegisterForRemoteNotificationsWithDeviceToken` is being called but notification banners aren't appearing when my server sends them. – drsanger73 Jun 27 '20 at 02:56

0 Answers0