如果您还没有设置AppDelegate
为,则将以下内容添加到方法中:UNUserNotificationCenterDelegate
didReceive
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = ["identifier":response.notification.request.identifier]
NotificationCenter.default.post(name: Notification.Name(rawValue: "userNotificationCenterDidReceiveResponse"), object: self, userInfo: userInfo)
completionHandler()
}
然后,您可以注册该“userNotificationCenterDidReceiveResponse”通知并使用标识符做任何您喜欢的事情。无论您的应用处于何种状态(包括未运行),这都有效。
如果这还不够清楚,我可以上传一个示例项目。