当我的设备收到通知时,我可以获取本地时间吗?我的意思是,如果我在后台模式下收到通知,当我点击它时,我想知道设备收到通知的时间,例如 10 秒之前。或者它仅在服务器推送时从有效负载进行配置?
最佳答案
如果您使用的是本地通知,您可以自行保存, 对于远程通知,告诉服务器人员向您发送通知时间, 然后当收到通知时,您可以保存详细信息
我正在分享下面的代码,如何在收到通知时获取响应,我正在使用此代码推送到其他 vc
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
let notificationTitle = response.notification.request.content.title
let info = response.notification.request.content.userInfo
let data = info["moduledata"] as? String ?? ""
switch notificationTitle {
case "Notice Added":
let vc = NoticeDetailViewController()
vc.noticeID = data
let navVC = UINavigationController(rootViewController: vc)
window?.rootViewController = navVC
case "Practicework Added":
let vc = HomeworkDetailViewController()
vc.id = data
let navVc = UINavigationController(rootViewController: vc)
window?.rootViewController = navVc
default:
print("")
}
completionHandler()
}
关于swift - Swift 中获取通知到来的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54861156/