手动设置时间和日期时,不会触发 iOS 10 中的重复每日本地通知? [英] Repeat Daily Local Notifications in iOS 10 does not get triggered when manually setting the Time and Date?

查看:64
本文介绍了手动设置时间和日期时,不会触发 iOS 10 中的重复每日本地通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过触发每日通知来测试 iOS 10 中的本地通知.

I'm trying to test local notifications in iOS 10 by trying to trigger a daily notification.

我正在使用以下示例项目:NotificationsUI-Demo

I am using the following sample project: NotificationsUI-Demo

在应用程序中是以下代码之一:

In the app is one of the following code:

let calendar = Calendar(identifier: .gregorian)
    let components = calendar.dateComponents(in: .current, from: date)
    let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)

    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

    let content = UNMutableNotificationContent()
    content.title = "Tutorial Reminder"
    content.body = "Just a reminder to read your tutorial over at appcoda.com!"
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "myCategory"

    if let path = Bundle.main.path(forResource: "logo", ofType: "png") {
        let url = URL(fileURLWithPath: path)

        do {
            let attachment = try UNNotificationAttachment(identifier: "logo", url: url, options: nil)
            content.attachments = [attachment]
        } catch {
            print("The attachment was not loaded.")
        }
    }

    let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)

    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("Uh oh! We had an error: \(error)")
        }
    }

let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

我将 repeat 的值从 false 更改为 true 以每天重复,因为根据描述 repeat参数:

I changed the value of repeats from false to true to repeat daily because according to the documentation describing the repeats parameter:

指定 false 以在触发器触发后取消安排通知.指定 true 会导致重复重新安排通知.

Specify false to unschedule the notification after the trigger fires. Specifying true causes the notification to be rescheduled repeatedly.

假设我在 2017 年 4 月 10 日下午 6:56 触发了今天的通知.下午 6 点 56 分,我按预期看到了本地通知.

Let's say I trigger a notification for today 4/10/2017 at 6:56pm. At 6:56pm, I see a local notification as expected.

当我尝试手动更改日期时时间通过设置->日期&时间到 2017 年 4 月 11 日下午 6 点 55 分,一到下午 6 点 56 分,我就完全看不到通知.

When I try to manually change the date & time via Settings->Date & Time to 4/11/2017 at 6:55pm, once 6:56pm rolls around, I do not see a notification at all.

不能这样测试吗?

我没有尝试在指定时间实际等待第二天查看是否弹出另一个通知,但我很好奇为什么这种测试方式不起作用?

I have not tried to actually wait the next day at the specified time to see if another notification pops up, but I'm curious as to why this way of testing does not work?

谢谢.

推荐答案

您正在调用带有日期和月份的 Daterepeat.因此,这个日期要到明年才会再次出现.因此,第二天将不会发送通知.通知的触发器必须仅包含使用 DateComponents 的小时和分钟值,以允许通知每天重复:

You are calling repeats on a Date with a day and month. Therefore, this date will not occur again until next year. Thus, no notification will be sent the following day. The trigger for your notification must only contain an hour and minute value using DateComponents to allow the notification to repeat every day:

var date = DateComponents()
date.hour = 11
date.minute = 41

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let content = UNNotificationContent()

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

这篇关于手动设置时间和日期时,不会触发 iOS 10 中的重复每日本地通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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