0

Summary: App launches without getting new data while a .foreground notification action code is still executing.

Edit for clarity: I’m referring only to local notifications here.

So far I’ve got notifications to display on time, action buttons to show below a notification, and the userInfo dictionary data to be passed correctly from the body of the notification to the didRecieve function in my AppDelegate.

However, the app seems to launch simultaneously with the code being executed in the notification action didRecieve switch statement as a result of which UserDefaults (which the didReceive function is saving to) complete saving with new values after the app has already launched with old values.

Is there any way to delay app launch until these UserDefaults are saved, or is there a better approach to this altogether.

There’s a scheduleNotification() function that sets up a content.userInfo.

The userNotificationCenter(_ center:didReceive:withCompletionHandler @escaping () -> Void) function receives this via response.notification.request.content.userInfo and everything is OK.

When the user clicks on one of the notification action buttons which has the .foreground option, it takes the above userInfo data and saves it to user defaults using UserDefaults.standard.set(:forKey).

Expected behaviour at this point: the app on launch should read the newly set UserDefaults data and update its UI accordingly.

What’s happening: the app launches with no regard whatsoever to the newly set User Defaults data. However, when two notifications are run, after running the second notification the app shows UI corresponding to the first notification which tells me that the notification didReceive function is doing its job, just not before the app launches.

  • 1
    You must get the notification from `launchOptions` dictionary. Let me find you some links. – NSGangster Jun 14 '19 at 19:42
  • [Here](https://stackoverflow.com/a/21611009/5153744) is good answer in objective-C you should be able to convert to swift. Keep in mind if your notification is not from a remote notification you might need a [different key](https://developer.apple.com/documentation/uikit/uiapplication/launchoptionskey) to access the dictionary values you are looking for – NSGangster Jun 14 '19 at 19:49
  • @NSGangster Thanks. This is a local notification (I’ve edited the question to reflect that) and the local notifications launch options key has been deprecated according to Apple docs. – V.H. Belvadi Jun 15 '19 at 01:47
  • I believe you need to use the `ContinueUserActivity` set of delegate methods then. I will try to post you some more links. – NSGangster Jun 17 '19 at 14:24

0 Answers0