Can I have an active countdown timer in my notification alert using UserNotifications in Swift 3

Hello everybody,

 

I’m using UserNotifications to listen some local notification in my application with Swift 3 and xcode.

I need to put a countdown timer displaying on notification’s little window or bar, counting  from 10 to 0 when a notification arrive, to do some action automatically after this 10 secondes.

 

Here is this part of code, when I recive a notification :

(...)
  
  switch eventCode
        {
       
        case Stream.Event.hasBytesAvailable:

          if (alert == 1)
          {
          let yes = UNNotificationAction(identifier: "yes", title: "send SOS", options: .foreground)
          let no =  UNNotificationAction(identifier: "no", title: "cancel", options: .foreground)
           
                    timer.invalidate()
                    time = 10
                    lbW.text = ("10")
                    lbW.textColor = UIColor.clear
           
                    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
                    lbW.textColor = UIColor.red
           
           
                let category = UNNotificationCategory(identifier: "cat", actions: [yes, no], intentIdentifiers: [], options: [])
                UNUserNotificationCenter.current().setNotificationCategories([category])
                let content_2 = UNMutableNotificationContent()
                content_2.title = "Title"

                content_2.body = lbW.text!
                content_2.sound = UNNotificationSound.default()
                content_2.categoryIdentifier = "cat"
                content_2.badge = nmbAcc as NSNumber
                let trigger_2 = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
                let request = UNNotificationRequest(identifier: "any", content: content_2, trigger: trigger_2)
                UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
           
}



 

And Here the timer function :

   func action()
    {
        if(time <= 0)
        {
            timer.invalidate()
            time = 10
            lbW.text = ("10")
            lbW.textColor = UIColor.clear
        }
        else
        {
        time -= 1
        lbW.text = String(time)
        lbW.textColor = UIColor.red
        }
    }
(...)




 

Timer work well, and notifications alerte correctly, but I can't see somewhere in my codes,

cause I don't know why the Body part of alert notification, (here on the line 27), don't display countdowt timer counting from 10 to 0 on notification's bar  ?

It just display the fixed timer number when the alert arrived. It just displays a counter sample, but not the active counter counting from 10 to 0.

Powered by WPeMatico

About

You may also like...

Comments are closed.