Swift 4通过计时器接收推送通知

问题描述 投票:0回答:1

我有userNotificationCenter willPresent 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 {

        }
    }
swift notifications
1个回答
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])
    }
}

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

要么

请告诉我你究竟在寻找什么

© www.soinside.com 2019 - 2024. All rights reserved.