Swift 4接收带有计时器的推送通知

时间:2018-08-31 10:52:25

标签: swift notifications

我有 userNotificationCenter 将要显示的 func()。我想要做的是...收到一次通知,然后每5分钟收到一次通知(如果存在)。我该如何执行?

userNotificationCenter:

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 {

        }
    }

1 个答案:

答案 0 :(得分:0)

尝试检查以下代码,据我了解,我认为您只需要在特定时间间隔后发送内容相同的通知

import UserNotifications

struct NotificationHandlerStruct {
    static var notifyTimer : Timer?
}

class LocalNotificationHandler: NSObject, UNUserNotificationCenterDelegate
{
    static var shared = LocalNotificationHandler()

    //MARK: Schedule Notification
    func scheduleNotification(Title title: String, Subtitle subtitle: String, BodyMessage body: String) {
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["gruveoCall"])
        let content = UNMutableNotificationContent()
        //adding title, subtitle, body and badge
        content.title = title
        content.subtitle = subtitle
        content.sound = UNNotificationSound.default()
        content.body = body
        content.badge = 0
        content.userInfo = ["type":"calling"]

        //getting the notification trigger
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
        //getting the notification request
        let request = UNNotificationRequest(identifier: "Call", content: content, trigger: trigger)
        //adding the notification to notification center
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

        if NotificationHandlerStruct.notifyTimer == nil {
            NotificationHandlerStruct.notifyTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { (timer) in
                self.sendNotification(NotificationContent: content)
            })
        }
        else{
            NotificationHandlerStruct.notifyTimer?.invalidate()
            NotificationHandlerStruct.notifyTimer = nil
        }

    }

    //MARK: Repeat Notification
    func sendNotification(NotificationContent content: UNMutableNotificationContent) {
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["gruveoCall"])
        //getting the notification trigger
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
        //getting the notification request
        let request = UNNotificationRequest(identifier: "Call", content: content, trigger: trigger)
        //adding the notification to notification center
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }

    //MARK: Stop Timer
    func stopTimer() {
        if NotificationHandlerStruct.notifyTimer != nil {
            NotificationHandlerStruct.notifyTimer?.invalidate()
            NotificationHandlerStruct.notifyTimer = nil
        }
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        //displaying the ios local notification when app is in foreground
        completionHandler([.alert, .badge, .sound])
    }
}

用户打开该特定通知只会使计时器无效

请告诉我您到底在寻找什么