Handling user notifications on iOS 10
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.
Related videos on Youtube
![Jan](https://i.stack.imgur.com/iOB4J.jpg?s=256&g=1)
Comments
-
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 whenCase 1
: the application is active and the push is receivedCase 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 theCase 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 almost 8 yearsIf 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 almost 8 yearsExactly. Thanks for the openradar link, I'll follow it
-
Jan almost 8 yearsStill not fixed in iOS 10 beta 7
-
Jan almost 8 yearsSame fir iOS 10 beta 8 :/
-
Jan almost 8 yearsSame 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 almost 8 yearsNeed to use UseNotification framework. I have implemented code for iOS 10 : stackoverflow.com/questions/39490605/…
-
fabb almost 8 yearsHas apple answered to the radar yet? They really should release an iOS bugfix version for that.
-
Jan almost 8 yearsApple closed it as duplicat and said that it is under investigation
-
fabb almost 8 yearsHas someone opened a Technical Support Incident about this with apple? developer.apple.com/support/technical
-
mathz almost 8 yearsFor 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 over 7 yearsdidReceiveRemoteNotification is never getting called in case 2 regardless iOS cersion. Instead: developer.apple.com/reference/uikit/uiapplicationdelegate/…
-
Vaisakh over 7 yearsi am still facing the same issue in iOS 10.1.1, does apple fixed this issue?
-
grane2212 over 7 yearsHas anyone seen this issue since iOS 10.2.1 release? Seems to be fixed.
-
mfaani about 7 yearsWhat's wrong with Adam's answer? Or why isn't his answer considered better? Can you please explain?
-
Jan about 7 yearsIn my initial question, I was not mentioning nor using UNNotifications framework at all. It simply appeared to be a iOS 10 bug
-
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 about 6 years@Jan Could you please tell me which method gets executed when the app is in background and receives user notification?
-
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 atUNNotifications
. This is out of scope of my original question though -
ArgaPK about 6 years@Jan Thank you, I will see it , but i was looking for the solution in User Notification framework.
-
Jan almost 8 yearshow did the application state changed on iOS10?
-
andresk almost 8 yearsWe 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 over 7 yearsit 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 over 7 yearsWhen was the 10.0.2 released?
-
Thomas Besnehard over 7 yearsiOS 10.0.2 had been release around October 14, 2016
-
vilanovi over 7 yearsI'm using iOS 10.0.2 and the issue is still happening. :(
-
Vaisakh over 7 yearsi am getting the same issue 10.1.1
-
Mason G. Zhwiti over 7 yearsIs 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 over 7 yearsHas anyone seen this issue since iOS 10.2.1 release?
-
dirtydanee over 7 years@grane2212 i am experiencing the issue today.
-
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 about 7 yearsIf 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? -
Jenny Tran almost 7 yearsYou can get it in
willPresentNotification
function @Honey -
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 almost 7 yearsYes, I tested it. The func
didReceiveRemoteNotification
isn't called withUserNotification
in iOS 10. -
mfaani almost 7 years@DungTran I'm sorry I meant
application:didReceiveRemoteNotification:fetchCompletionHandler
. <-- This is called! -
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 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 about 6 years@ArgaPK didReceiveRemoteNotification
-
ArgaPK about 6 years@AdamBardon didreceiveRemoteNotification is not executing and it is also deprecated. i am using user notification framework
-
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 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 about 6 years@ArgaPK as I said, didReceiveRemoteNotification works for me
-
ArgaPK about 6 yearsWhich method gets executed when the app is in background and receives notification? (User Notification Framework)
-
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 about 6 years@ArgaPK simulator might be the issue, as it is unable to receive push notifications, try with real device