通知中心和更改时区 [英] Notification Center and changing timezones

查看:112
本文介绍了通知中心和更改时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 4& > iOS 10.0

我想安排在某个日期和给定时间(比如说下午3点)的本地通知.无论我在哪个时区,我都希望通知总是在下午3点触发(根据时区自动重新安排通知).

以前,您可以调整UILocalNotifications时区来实现此目标,就像此SO帖子中所完美解释的那样.但是,在>iOS 10.0中,不推荐使用UILocalNotifications.

这是我的代码:

func scheduleNotification(title: String, message: String, atDate: Date){

    let center = UNUserNotificationCenter.current()

    // Create content
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = message
    content.sound = UNNotificationSound.default()

    // Create trigger
    let calendar = Calendar(identifier: .gregorian)
    let triggerDate = calendar.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: atDate)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)

    // Create identifier
    let identifier = "\(UUID())"

    // Create request & add to center
    let request = UNNotificationRequest(identifier: identifier,
                                        content: content,
                                        trigger: trigger)
    center.add(request, withCompletionHandler: { (error) in
    })
  }

问题: 如何通过更改时区使通知触发器正确?

解决方案

因此,我设法使其正常运行. triggerDate具有一个timeZone变量,该变量自动为nil,与UILocalNotification完全一样.

triggerDate.timeZone的行为与UILocalNotification.timeZone完全一样(中所述的行为,与问题中提到的相同).

它似乎无法在模拟器上运行的原因之一是因为更改时区时我没有重新启动模拟器.重新启动将使一切正常工作.

注意:也许不是强制性的重启,但是由于不清楚正在运行的模拟器需要多长时间来检测新时区,因此我认为重启是最有效的解决方案.

Swift 4 & >iOS 10.0

I want to schedule a local notification at a certain date and at a given time (let's say 3PM). I want the notifications to always be fired at 3PM, whatever the timezone I am in (automatic rescheduling of notifications according to timezones).

Previously, you could tweak UILocalNotifications' time zone to achieve exactly this, like perfectly explained in this SO post. However, in >iOS 10.0, UILocalNotifications is deprecated.

Here is my code:

func scheduleNotification(title: String, message: String, atDate: Date){

    let center = UNUserNotificationCenter.current()

    // Create content
    let content = UNMutableNotificationContent()
    content.title = title
    content.body = message
    content.sound = UNNotificationSound.default()

    // Create trigger
    let calendar = Calendar(identifier: .gregorian)
    let triggerDate = calendar.dateComponents([.year,.month,.day,.hour,.minute,.second,], from: atDate)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)

    // Create identifier
    let identifier = "\(UUID())"

    // Create request & add to center
    let request = UNNotificationRequest(identifier: identifier,
                                        content: content,
                                        trigger: trigger)
    center.add(request, withCompletionHandler: { (error) in
    })
  }

Question: How do you make the notification triggers properly with changing timezones ?

解决方案

So, I managed to make it work. The triggerDate has a timeZone variable which is automatically nil, exactly like UILocalNotification.

triggerDate.timeZone behaves exactly like UILocalNotification.timeZone (behaviour described in this post, the same as mentioned in the question).

One of the reason it did not seem to work on the simulator was because I was not restarting the simulator when changing the timezone. Restarting will make everything work as expected.

Nota bene: Maybe a restart is not mandatory but since it's not obvious how much time a running simulator will take to detect the new timezone, I think restarting it is the most efficient solution.

这篇关于通知中心和更改时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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