Detect if app was launched by opening a local notification on iOS10+
Asked Answered
C

1

7

I am currently migrating an app to the new UserNotifications framework. I'm stuck at detecting if the app was launched due to the user opening a local notification. The test case is:

  1. Schedule a local notification
  2. Manually close the app
  3. Setup Xcode to automatically attach when app is launched
  4. Wait for the notification to show and open it (default action)

The problem is that in this case userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: is not called. In application:didFinishLaunchingWithOptions: there is a key UIApplicationLaunchOptionsLocalNotificationKey which contains an object of type UIConcreteLocalNotification which is a subclass of UILocalNotification. However, as part of the old notification system, UILocalNotification is deprecated and we are not supposed to use it. I dug in the documentation and the web and did not find an answer to my question:

How do I find if an app was launched due to a local notification?

How do I obtain that notification?

Castano answered 19/10, 2017 at 6:31 Comment(0)
K
9

Make sure you're assigning your implementation of UNUserNotificationCenterDelegate to UNUserNotificationCenter.current() before your app finishes launching, as it says in the documentation.

Then you should be getting a call to your UNUserNotificationCenterDelegate's userNotificationCenter(_:didReceive:withCompletionHandler:) at app launch with the response object, which contains the original notification. For me, it happened some time after my application(_:didFinishLaunchingWithOptions:) was called.

Kaitlynkaitlynn answered 10/1, 2018 at 19:8 Comment(5)
Hm, looks like this was the problem. I did set the delegate but after some refactoring it was moved to a later stage of app launch instead of 'application(_:didFinishLaunchingWithOptions:)'. Will check it tomorrow and let you know of the result.Castano
I too am wondering about this. I receive the UNUserNotificationCenterDelegate's didReceiveNotificationResponse message, however.... how can I detect if the didReceiveNotificationResponse was received as a result of launching the app because the notification was clicked in the notification center while the app wasn't running? In iOS8 (the deprecated notification stuff), I can find examples that detect this, but not so much in iOS10. Shall I just compare a time difference between didFinishLaunchingWithOptions and didReceiveNotificationResponse?Ginny
I tested this, and I learned that on iOS 10+, your didFinishLaunchingWithOptions delegate will be invoked w/ an Options dictionary that has the key LaunchOptionsLocalNotificationKey in it w/ a UILocalNotification object in it, EVEN IF the notification was created using the NEW UNUserNotificationCenter and a UNNotificationRequest.Ginny
@Ginny Yeah I noticed that too, though LaunchOptionsLocalNotificationKey and UILocalNotification are deprecated so I imagine it'll go away or become infested with bugs. But it works now, ship it! :)Kaitlynkaitlynn
You can determine if the notification was opened or dismissed in the method userNotificationCenter by checking response.actionIdentifier. (Swift jargon. ObjC should be similar)Snail

© 2022 - 2024 — McMap. All rights reserved.