iOS 10 UNNotificationAction 从后台打开应用程序

13

当用户接收到通知并且我的应用程序在后台运行时,我该如何打开我的应用程序?当然,点击通知本身会打开应用程序,但是如何处理自定义通知操作呢?

我已经实现了“UNUserNotificationCenterDelegate”和“userNotificationCenter(_, didReceive, ...)”。

只需要一个代码即可打开我的应用程序并执行特定操作(例如转到特定视图)。

如果通知处理程序能够识别通知并根据点击操作在iPhone应用程序或Apple Watch应用程序中打开,则更好。


除了这个问题和建议的答案之外,我注意到 https://developer.apple.com/documentation/usernotifications/unnotificationactionoptions/1648192-foreground 上说:“对于需要用户进一步与您的应用程序交互的操作,请使用此选项。不要仅仅使用此选项将您的应用程序带到前台。”有什么想法可以解释这个区别以及为什么它很重要吗? - Simon Pickup
2个回答

22

好的,我已经找到了解决方案。我们需要设置UNNotificationActionOptions :)

UNNotificationActionOptions.foreground

12

这是一个Swift 3/4的示例(因为我不知道如何设置此选项):重要的是UNNotificationActionUNNotificationActionOptions

let myAction = UNNotificationAction(identifier: "myActionIdentifier", title: "MyAction", options: [.foreground])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接