Handling user notifications on iOS 10

20,430

Solution 1

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.

Solution 2

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)
}

Solution 3

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.
Share:
20,430

Related videos on Youtube

Jan
Author by

Jan

Doing iOS @ N26, previously @ HERE, Fyber and 360dialog

Updated on October 07, 2020

Comments

  • Jan
    Jan over 3 years

    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

    • Felipe Sabino
      Felipe Sabino almost 8 years
      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. openradar.me/27822963
    • Jan
      Jan almost 8 years
      Exactly. Thanks for the openradar link, I'll follow it
    • Jan
      Jan almost 8 years
      Still not fixed in iOS 10 beta 7
    • Jan
      Jan almost 8 years
      Same fir iOS 10 beta 8 :/
    • Jan
      Jan almost 8 years
      Same for iOS 10 GM :/ Does it mean that on iOS 10 we have to use a callback that is deprecated on iOS 10 😡
    • Ashish Shah
      Ashish Shah almost 8 years
      Need to use UseNotification framework. I have implemented code for iOS 10 : stackoverflow.com/questions/39490605/…
    • fabb
      fabb almost 8 years
      Has apple answered to the radar yet? They really should release an iOS bugfix version for that.
    • Jan
      Jan almost 8 years
      Apple closed it as duplicat and said that it is under investigation
    • fabb
      fabb almost 8 years
      Has someone opened a Technical Support Incident about this with apple? developer.apple.com/support/technical
    • mathz
      mathz almost 8 years
      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.
    • user523234
      user523234 over 7 years
      didReceiveRemoteNotification is never getting called in case 2 regardless iOS cersion. Instead: developer.apple.com/reference/uikit/uiapplicationdelegate/…
    • Vaisakh
      Vaisakh over 7 years
      i am still facing the same issue in iOS 10.1.1, does apple fixed this issue?
    • grane2212
      grane2212 over 7 years
      Has anyone seen this issue since iOS 10.2.1 release? Seems to be fixed.
    • mfaani
      mfaani about 7 years
      What's wrong with Adam's answer? Or why isn't his answer considered better? Can you please explain?
    • Jan
      Jan about 7 years
      In my initial question, I was not mentioning nor using UNNotifications framework at all. It simply appeared to be a iOS 10 bug
    • ArgaPK
      ArgaPK about 6 years
      @AshishShah Could you please tell me which method gets executed when the app is in background and receives user notification?
    • ArgaPK
      ArgaPK about 6 years
      @Jan Could you please tell me which method gets executed when the app is in background and receives user notification?
    • Jan
      Jan about 6 years
      @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
    • ArgaPK
      ArgaPK about 6 years
      @Jan Thank you, I will see it , but i was looking for the solution in User Notification framework.
  • Jan
    Jan almost 8 years
    how did the application state changed on iOS10?
  • andresk
    andresk almost 8 years
    We faced problems like the ones described here: 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...
  • Thomas Besnehard
    Thomas Besnehard over 7 years
    it seam to be corrected already in iOS 10.0.2. If anyone got some official information about it, I'll be late to see it !
  • Jan
    Jan over 7 years
    When was the 10.0.2 released?
  • Thomas Besnehard
    Thomas Besnehard over 7 years
    iOS 10.0.2 had been release around October 14, 2016
  • vilanovi
    vilanovi over 7 years
    I'm using iOS 10.0.2 and the issue is still happening. :(
  • Vaisakh
    Vaisakh over 7 years
    i am getting the same issue 10.1.1
  • Mason G. Zhwiti
    Mason G. Zhwiti over 7 years
    Is it possible that the app must be running on 10.1 AND the app must have been built for iOS 10 with Xcode 8, in order for the bad behavior to be absolved?
  • grane2212
    grane2212 over 7 years
    Has anyone seen this issue since iOS 10.2.1 release?
  • dirtydanee
    dirtydanee over 7 years
    @grane2212 i am experiencing the issue today.
  • grane2212
    grane2212 over 7 years
    @dirtydanee I think this still might be an issue. I haven't tried reproducing this issue recently. Will need to report this to apple.
  • mfaani
    mfaani about 7 years
    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:fetchCompletionHand‌​ler:]. Is that correct?
  • Jenny Tran
    Jenny Tran almost 7 years
    You can get it in willPresentNotification function @Honey
  • mfaani
    mfaani almost 7 years
    @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
  • Jenny Tran
    Jenny Tran almost 7 years
    Yes, I tested it. The func didReceiveRemoteNotification isn't called with UserNotification in iOS 10.
  • mfaani
    mfaani almost 7 years
    @DungTran I'm sorry I meant application:didReceiveRemoteNotification:fetchCompletionHand‌​‌ler. <-- This is called!
  • ArgaPK
    ArgaPK about 6 years
    @andresk Could you please tell me which method gets executed when the app is in background and receives user notification?
  • ArgaPK
    ArgaPK about 6 years
    @DungTran Could you please tell me which method gets executed when the app is in background and receives user notification?
  • Adam Bardon
    Adam Bardon about 6 years
    @ArgaPK didReceiveRemoteNotification
  • ArgaPK
    ArgaPK about 6 years
    @AdamBardon didreceiveRemoteNotification is not executing and it is also deprecated. i am using user notification framework
  • Adam Bardon
    Adam Bardon about 6 years
    @ArgaPK oh, you're right, I'm still supporting iOS 10.3 so that's probably why it's not showing deprecated for me
  • ArgaPK
    ArgaPK about 6 years
    @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?
  • Adam Bardon
    Adam Bardon about 6 years
    @ArgaPK as I said, didReceiveRemoteNotification works for me
  • ArgaPK
    ArgaPK about 6 years
    Which method gets executed when the app is in background and receives notification? (User Notification Framework)
  • ArgaPK
    ArgaPK about 6 years
    @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?
  • Adam Bardon
    Adam Bardon about 6 years
    @ArgaPK simulator might be the issue, as it is unable to receive push notifications, try with real device