Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 248
AXE Avatar asked Oct 19 '17 06:10

AXE


1 Answers

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.

like image 145
yood Avatar answered Oct 09 '22 14:10

yood