13

How can I open my app, when user receive notification and my app was in background? Of course tap on notification itself would open app, but how to handle it with custom notification action?

I have implemented UNUserNotificationCenterDelegate and userNotificationCenter(_, didReceive, ...) already

Just need a code which would open my app and do specific action (for example go to specific view)

It would be nice if notification handler recognise that notification and open iPhone app or Apple Watch app depends on where action was tapped..

mfaani
  • 33,269
  • 19
  • 164
  • 293
Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
  • Further to this question and the suggested answers, I note that https://developer.apple.com/documentation/usernotifications/unnotificationactionoptions/1648192-foreground says: "Use this option for actions that require the user to interact further with your app. Do not use this option simply to bring your app to the foreground." Any ideas what distinction this is making and why it matters? – Simon Pickup Jan 16 '21 at 04:57

2 Answers2

22

Ok i have found the solution. We have to set UNNotificationActionOptions :)

UNNotificationActionOptions.foreground
Adam Smaka
  • 5,977
  • 3
  • 50
  • 55
12

A Swift 3/4 example (because I did not know how to set this option): important are the UNNotificationActionOptions of the UNNotificationAction.

let myAction = UNNotificationAction(identifier: "myActionIdentifier", title: "MyAction", options: [.foreground])
palme
  • 2,499
  • 2
  • 21
  • 38