热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS10-每隔“x”分钟重复一次通知-iOS10-Repeatingnotificationsevery“x”minutes

IniOS10,howcanIsetlocalnotificationstorepeatinminutesstartingfromaparticulardateti

In iOS 10, how can I set local notifications to repeat in minutes starting from a particular date/time.

在iOS 10中,如何设置从特定日期/时间开始在几分钟内重复的本地通知。

For example, trigger local notification every 15 minutes starting from 11 AM on 8th September? Assume that, below, dateTimeReminder.date has 09/08 11 AM.

例如,从9月8日上午11点开始,每15分钟触发一次本地通知?假设在下面,dateTimeReminder.date有09/08上午11点。

let dateStart = self.dateTimeNotif.date
let notifTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: dateStart), repeats: true)
let notificatiOnRequest= UNNotificationRequest(identifier: "MYNOTIF", content: notifContent, trigger: notifTrigger)

UNUserNotificationCenter.current().add(notificationRequest, withCompletionHandler: nil)

With the above code, I have a possibility to schedule at a particular minute of every hour, at a particular hour of each day and so on. But how do I turn it into "every "x" minutes"? Any help is appreciated.

通过上面的代码,我有可能在每小时的特定时刻,每天的特定时刻安排,等等。但是如何将其变成“每”x“分钟”?任何帮助表示赞赏。

Similar question - How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

类似的问题 - 如何在iOS 10 UserNotifications上设置NSCalendarUnitMinute repeatInterval?

6 个解决方案

#1


3  

As you are already aware, you can schedule maximum of 64 notifications per app. If you add more than that, the system will keep the soonest firing 64 notifications and will discard the other.

您已经知道,每个应用最多可以安排64个通知。如果你添加更多,系统将保持最快的64个通知,并将丢弃另一个。

One way to make sure all notifications get scheduled is to schedule the first 64 notifications first, and then on regular time intervals (may be on every launch of the app or each time a notification fires) check for the number of notifications scheduled and if there are less than 64 notifications, lets say n notifications, then schedule the next (64 - n) notifications.

确保所有通知都已安排的一种方法是先安排前64个通知,然后按照常规时间间隔(可能是每次启动应用程序或每次通知触发时)检查计划的通知数量以及是否有少于64个通知,让我们说n个通知,然后安排下一个(64-n)通知。

int n = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
int x = 64 - n;
// Schedule the next 'x' notifications

for rest add a NSTimer for X minutes to come and set the notification.

休息时间为X分钟添加NSTimer并设置通知。

override func viewDidLoad() {
    super.viewDidLoad()
    //Swift 2.2 selector syntax
    var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: #selector(MyClass.update), userInfo: nil, repeats: true)
    //Swift <2.2 selector syntax
    var timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self, selector: "update", userInfo: nil, repeats: true)
}

// must be internal or public. 
func setNotification() {
    // Something cool
}

Make sure you remove old and not required notifications.

确保删除旧的和不需要的通知。

#2


2  

After searching around quite a bit, I've come to the conclusion that Swift 3, at this point, doesn't support this feature. For everyone looking for this functionality, I'd suggest using UILocalNotification for now (although deprecated in iOS 10), but later migrate to UNUserNotification once it supports this feature. Here are some additional questions and resources that have helped me to reach this conclusion. Also, please follow all the answers and comments in this thread to get more insight into which particular scenario it talks about.

经过大量搜索后,我得出的结论是,此时Swift 3不支持此功能。对于每个寻找此功能的人,我建议现在使用UILocalNotification(虽然在iOS 10中已弃用),但是一旦支持此功能,稍后会迁移到UNUserNotification。以下是一些帮助我得出这个结论的其他问题和资源。另外,请遵循此主题中的所有答案和评论,以便更深入地了解它所涉及的特定方案。

  • It is usually a bad idea to use deprecated APIs. As a general practice, migrate to new APIs as soon as possible. The above solution is NOT recommended as a permanent solution.
  • 使用已弃用的API通常是个坏主意。作为一般做法,请尽快迁移到新的API。不建议将上述解决方案作为永久解决方案。

Local Notification every 2 week

每两周一次本地通知

http://useyourloaf.com/blog/local-notifications-with-ios-10/

https://github.com/lionheart/openradar-mirror/issues/14941

#3


1  

The link you shared and the new details you added you need to save the last hour and minute of time for which notification got scheduled. Then when the timer ticks to next minute, there change the hour and minute

您共享的链接以及您添加的新详细信息需要保存计划通知的最后一小时和一小时。然后当计时器滴答到下一分钟时,会改变小时和分钟

var date = DateComponents()
date.hour = 8
date.minute = 30

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let cOntent= UNNotificationContent()
// edit your content

let notification = UNNotificationRequest(identifier: "myNotification", content: content, trigger: trigger)

#4


1  

Swift 3/4 and iOS 10/11:

Swift 3/4和iOS 10/11:

According with this bug seems there is no way to use DateComponents() to repeat correctly a local notification.

根据这个bug似乎没有办法使用DateComponents()来正确重复本地通知。

Instead of this method you can change your trigger with TimeInterval (this method works if you interval is major than 60 seconds):

您可以使用TimeInterval更改触发器而不是此方法(如果间隔大于60秒,则此方法有效):

let thisTime:TimeInterval = 60.0 // 1 minute = 60 seconds

// Some examples:
// 5 minutes = 300.0
// 1 hour = 3600.0
// 12 hours = 43200.0
// 1 day = 86400.0
// 1 week = 604800.0

let trigger = UNTimeIntervalNotificationTrigger(
            timeInterval: thisTime,
            repeats: true)

#5


0  

I think this thread has what you're looking for.

我认为这个主题有你想要的东西。

Do something every x minutes in Swift

在Swift中每隔x分钟做一些事情

The '60.0' represents seconds between the code running. Simply change that to x minutes * 60

'60 .0'表示代码运行之间的秒数。只需将其更改为x分钟* 60

#6


0  

You can first use the UNCalendarNotificationTrigger to trigger the notification at a particular date and set its repeat property to false so that it notifies only once. When you receive that notification, use the UNTimeIntervalNotificationTrigger API to set the time interval at which you want to trigger the local notification

您可以先使用UNCalendarNotificationTrigger在特定日期触发通知,并将其repeat属性设置为false,以便它只通知一次。收到该通知后,使用UNTimeIntervalNotificationTrigger API设置要触发本地通知的时间间隔


推荐阅读
  • 在使用 iOS 应用时,遇到网络请求错误是常见的问题。本文将探讨两种常见的错误代码 -1003 和 -1001,并提供详细的解释和解决方案。 ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 本文介绍如何在 Android 中通过代码模拟用户的点击和滑动操作,包括参数说明、事件生成及处理逻辑。详细解析了视图(View)对象、坐标偏移量以及不同类型的滑动方式。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文详细介绍了 GWT 中 PopupPanel 类的 onKeyDownPreview 方法,提供了多个代码示例及应用场景,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Alluxio 1.5.0 版本发布:增强功能与优化
    Alluxio 1.5.0 开源版本引入了多项新特性和改进,旨在提升数据访问速度和系统互操作性。 ... [详细]
author-avatar
庾事镁
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有