26

I have troubles determining when the user taps on a user push notification on iOS 10.

So far, I have been using the -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] which is called when

  • Case 1: the application is active and the push is received
  • Case 2: when the user launched the app after taping a received notification

This method comments explicitly say

Note that this behavior is in contrast to application:didReceiveRemoteNotification:, which is not called in those cases, and which will not be invoked if this method is implemented.

All this work as expected.

Now iOS 10 deprecated this delegate method and introduced the UserNotification framework which I cannot use because I'm still targeting iOS 8 and 9.

When my app is running on iOS 10 and a push is received while the app is active (Case 1), the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is called correctly.

Again on iOS 10, when the user starts the app by tapping a notification (Case 2) this method is not called.

I realise that when I implement the older -[UIApplicationDelegate application:didReceiveRemoteNotification:] it is the one that gets called in the Case 2

On iOS 8 and 9, in the Case 2 it is the -[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] method is called.

Does it mean that I have to update my application and implement the older delegate just for iOS 10?

So the question is, what is the proper implementation of handling the user interaction of a received push on iOS 10 without using the UserNotification framework.

cheers, Jan

Jan
  • 7,444
  • 9
  • 50
  • 74
  • 1
    If you compiled the app for iOS 9 it should be backwards compatible and therefore you could, in theory, disregard the deprecation notice because you would not be targeting iOS 10, but iOS 9 where this methods should work. I am facing the same problem now and there is already an openradar for that, I suggest you keep an eye on it as it seems that this behavior is a bug on iOS 10 beta releases. http://www.openradar.me/27822963 – Felipe Sabino Aug 15 '16 at 19:13
  • Exactly. Thanks for the openradar link, I'll follow it – Jan Aug 16 '16 at 06:31
  • 1
    Still not fixed in iOS 10 beta 7 – Jan Aug 20 '16 at 09:59
  • 1
    Same fir iOS 10 beta 8 :/ – Jan Aug 27 '16 at 08:14
  • 1
    Same for iOS 10 GM :/ Does it mean that on iOS 10 we have to use a callback that is deprecated on iOS 10 – Jan Sep 08 '16 at 13:02
  • 1
    Need to use UseNotification framework. I have implemented code for iOS 10 : http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10?answertab=votes#tab-top – Ashish Shah Sep 15 '16 at 17:14
  • Has apple answered to the radar yet? They really should release an iOS bugfix version for that. – fabb Sep 16 '16 at 06:03
  • Apple closed it as duplicat and said that it is under investigation – Jan Sep 16 '16 at 06:04
  • Has someone opened a Technical Support Incident about this with apple? https://developer.apple.com/support/technical/ – fabb Sep 16 '16 at 06:50
  • For us it is happening with the official iOS 10.0.1 release. Unfortunately we did not come across this earlier. I opened a Technical Support Incident, hopefully Apple answer soon. – mathz Sep 16 '16 at 08:36
  • didReceiveRemoteNotification is never getting called in case 2 regardless iOS cersion. Instead: https://developer.apple.com/reference/uikit/uiapplicationdelegate/1657534-launch_options_keys – user523234 Oct 19 '16 at 04:59
  • i am still facing the same issue in iOS 10.1.1, does apple fixed this issue? – Vaisakh Nov 07 '16 at 06:02
  • Has anyone seen this issue since iOS 10.2.1 release? Seems to be fixed. – grane2212 Feb 01 '17 at 19:46
  • What's wrong with Adam's answer? Or why isn't his answer considered better? Can you please explain? – mfaani Jun 14 '17 at 03:26
  • In my initial question, I was not mentioning nor using UNNotifications framework at all. It simply appeared to be a iOS 10 bug – Jan Jun 14 '17 at 08:12
  • @AshishShah Could you please tell me which method gets executed when the app is in background and receives user notification? – ArgaPK Apr 18 '18 at 12:47
  • @Jan Could you please tell me which method gets executed when the app is in background and receives user notification? – ArgaPK Apr 18 '18 at 12:47
  • @ArgaPK without using the `UNNotifications`, no method is called unless the user taps on the notification. If you want to monitor when the notification is received when the app is in the background, have a look at `UNNotifications`. This is out of scope of my original question though – Jan Apr 19 '18 at 06:41
  • @Jan Thank you, I will see it , but i was looking for the solution in User Notification framework. – ArgaPK Apr 19 '18 at 06:59

3 Answers3

24

Swift code for iOS 10:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.currentNotificationCenter()
        center.delegate = self
    }

    // ...

    return true
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

    print(response.notification.request.content.userInfo)        
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

    print(notification.request.content.userInfo)
}
Adam Bardon
  • 3,829
  • 7
  • 38
  • 73
  • If I understand this all correctly. For iOS 10: the 2 delegate methods don't have a callback for when the user recieves a notification( doesn't tap) and still you must use the non-deprrcated: `-[AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:]`. Is that correct? – mfaani Jun 14 '17 at 03:29
  • 1
    You can get it in `willPresentNotification` function @Honey – Jenny Tran Aug 18 '17 at 06:43
  • @DungTran that is correct. I asked this question a while ago. You would get 2 callbacks upon receiving if app is in foreground. I just think the call acks are semantically different. `didReceiveRemoteNotitication` is just purely for receiving...its more for the purpose of **updating** your app. `willPresent` is more for **notifying** – mfaani Aug 18 '17 at 09:56
  • Yes, I tested it. The func `didReceiveRemoteNotification` isn't called with `UserNotification` in iOS 10. – Jenny Tran Aug 18 '17 at 09:59
  • @DungTran I'm sorry I meant `application:didReceiveRemoteNotification:fetchCompletionHand‌ler`. <-- This is called! – mfaani Aug 18 '17 at 13:00
  • @DungTran Could you please tell me which method gets executed when the app is in background and receives user notification? – ArgaPK Apr 18 '18 at 12:49
  • @ArgaPK _didReceiveRemoteNotification_ – Adam Bardon Apr 18 '18 at 12:58
  • @AdamBardon didreceiveRemoteNotification is not executing and it is also deprecated. i am using user notification framework – ArgaPK Apr 18 '18 at 13:04
  • @ArgaPK oh, you're right, I'm still supporting iOS 10.3 so that's probably why it's not showing deprecated for me – Adam Bardon Apr 18 '18 at 13:08
  • @AdamBardon In User Notification , local /remote notifications handling are same, now if the app is in foreground and we receive notification than will present delegate method of user notification will execute, and if user taps on notification or action associated with that notification than did receive delegate method of user notification will execute , but i wanted to know that how to perform a task when app is in background and receives notification? – ArgaPK Apr 18 '18 at 13:21
  • @ArgaPK as I said, _didReceiveRemoteNotification_ works for me – Adam Bardon Apr 18 '18 at 13:24
  • @AdamBardon but it is not working now, both didReceiveRemoteNotification with and without completion handler is not working? well i should also mention that i am right checking it in simulator, will it be executed on real iOS device? – ArgaPK Apr 18 '18 at 13:27
  • @ArgaPK simulator might be the issue, as it is unable to receive push notifications, try with real device – Adam Bardon Apr 18 '18 at 13:29
15

We were facing the same problem here and we were only able to solve this problem on iOS 10 GM release by using the code on the answer given here: https://forums.developer.apple.com/thread/54332

     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {  

           NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
           if (version.majorVersion == 10 && version.minorVersion == 0) {
              [self application: application 
   didReceiveRemoteNotification: userInfo    
         fetchCompletionHandler: ^(UIBackgroundFetchResult result) { 

            }];
       }

With this fix our code started working again both on iOS 9 and 10.

We also had to change the way we handle application state behavior (UIApplicationStateActive, UIApplicationStateInactive and UIApplicationStateBackground) on push notifications, as it seems it also changed on iOS 10

EDIT:

  • It seems that application state behavior is back to normal on latest iOS 10 versions.
andresk
  • 2,845
  • 1
  • 13
  • 17
  • how did the application state changed on iOS10? – Jan Sep 12 '16 at 06:32
  • We faced problems like the ones described here: https://forums.developer.apple.com/thread/54415 but I haven't tested on GM yet, so I'm not able to confirm if this is still a problem... – andresk Sep 12 '16 at 14:57
  • @andresk Could you please tell me which method gets executed when the app is in background and receives user notification? – ArgaPK Apr 18 '18 at 12:48
13

This has been fixed in iOS 10.1 Beta 1 !!

The -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] is correctly called when the user taps on a notification.

Jan
  • 7,444
  • 9
  • 50
  • 74