0

Here's my sample app

At the left you can see the app delegate, on the right my view controller

I've had some trouble with this in another app and I'm trying to figure out what I'm doing wrong; It works like this:

  1. I push a button that schedules the notification
  2. I quit the app and close it
  3. I wait for the notification to arrive
  4. I restart the app through Xcode

The thing is: even though the didReceiveRemoteNotification()-function should be called, I still get false value when starting the app which shows me, that for any reason this method does not get called. Why? Can you help me?

Carl Henretti
  • 457
  • 2
  • 7
  • 17

1 Answers1

2

Method didReceiveRemoteNotification() is not supposed to be called for local notifications... It's only for remote notifications (Push Notifications).

Use UNUserNotificationCenterDelegate delegate method

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

See details here https://stackoverflow.com/a/44142742/1825618

Get delivered notifications (Only the ones Currently being displayed in notification center)

UNUserNotificationCenter.current().getDeliveredNotifications {
    (notifications) in
    // your code
}
Bilal
  • 18,478
  • 8
  • 57
  • 72
  • already tried that... my problem now is rather that the function is not working if I access the app through tapping on the app icon. It works only if I access the app through the notification itself. Do you know if I can generally detect wether or not a notification has arrived? – Carl Henretti May 28 '17 at 15:48
  • 1
    thats expected behaviour if you launch the app from the app icon, delegate method won't be called. May be this will help you, by the time you open the app using app icon and your app notification is still being displayed in Notification Center, you can use this function to get all the delivered notifications `getDeliveredNotificationRequests:completionHandler:` – Bilal May 28 '17 at 15:55
  • Thanks! And where can I call that function? – Carl Henretti May 28 '17 at 16:01