我有userNotificationCenter willPresent func()。我想要做的是......收到一次通知,然后每 5 分钟收到一个通知(如果存在)。我怎么能做到这一点?
用户通知中心:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
//checkUser - current account ID
//userID - from user
var timer = Timer()
let userID : String = (EVTUser.user?.id)!
if let checkUser = notification.request.content.userInfo["user_id"]{
if userID != checkUser as! String{
guard let _ = UIViewController.visibleChatViewController() else {
completionHandler([.badge, .alert, .sound])
return
}
EVTChatLogController.receiveMessage(notification: notification.request.content)
//Push display
if isTransitionChat {
isTransitionChat = false
completionHandler([.badge, .alert, .sound])
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
else{
return
}
}
else{
guard let _ = UIViewController.visibleChatViewController() else {
completionHandler([.badge, .alert, .sound])
return
}
EVTChatLogController.receiveMessage(notification: notification.request.content)
if isTransitionChat {
isTransitionChat = false
completionHandler([.badge, .alert, .sound])
AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
}
更新: 我尝试使用此代码但仍然没有成功...
let uuidString = UUID().uuidString
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (5*60), repeats: true)
let request = UNNotificationRequest(identifier: uuidString, content: notification.request.content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
}
}