在特定时间触发后如何重复本地通知 [英] How to repeat local notification once fired at a particular time

查看:170
本文介绍了在特定时间触发后如何重复本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个闹钟应用,目前我可能只是误会.我想在特定时间启动本地通知,然后每隔一段时间重复一次.我已经有了一些想法,这个( https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/)程序与我想要的程序接近,但我希望它在某个时间关闭然后重复间隔一个时间.

I am currently making an alarm clock app and I am currently stuck at possibly just a misunderstanding. I want to launch a local notification at a specific time and repeat at a time interval. I have been getting a couple ideas and this (https://makeapppie.com/2017/01/31/how-to-repeat-local-notifications/) program is close to what I want it to be but I want it to go off at a certain time and then repeat at a single interval.

用户输入信息后,程序将吐出:

After user inputs information, the program will spit out:

timeIntervalSeconds,用户希望通知的时间间隔(以秒为单位)

timeIntervalSeconds, the interval that user wants the notifications in (in seconds)

timeStart,通知将在何时开始

timeStart, when the notifications will start

timeEnd,如果用户不停止通知,通知将在何时结束

timeEnd, when the notifications will end if the user doesn't stop them

任何建议将不胜感激.

谢谢!

推荐答案

要在特定时间启动通知,请使用以下代码.

To launch notification on specific time use below code.

let gregorian = Calendar(identifier: .gregorian)
                    let now = Date()
                    var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)

                    // Change the time to 10:00:00 in your locale
                    components.hour = 10
                    components.minute = 0
                    components.second = 0

                    let date = gregorian.date(from: components)!

                    let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
                    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)

                    let request = UNNotificationRequest(identifier: CommonViewController.Identifier, content: content, trigger: trigger)

要在特定时间间隔重复发送通知,请在触发器中使用以下代码.

And to repeat notifications on specific time interval use below code in trigger.

 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)  // it repeats after every 2 minutes

这篇关于在特定时间触发后如何重复本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆