Swift 3本地通知未触发 [英] Swift 3 Local notifications not firing

查看:55
本文介绍了Swift 3本地通知未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下设置,并且根本没有发出任何通知.

I have the following setup and no notification are firing at all.

基于堆栈上的其他类似问题,我为每个请求添加了唯一标识符,并且还将正文添加到了内容中.

Based on other similar questions on the stack, I've put in a unique identifier for each request and I've also added the body to the content.

我有这个功能,需要用户的许可.

I've got this function which requests permission from user.

    func sendIntNotifications()
        {
            //1.  Request permission from the user
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound], completionHandler:
                {
                    (granted, error) in

                    if granted
                    {
                        print ("Notification access granted")
                    }
                    else
                    {
                        print(error?.localizedDescription)
                    }

                    UNUserNotificationCenter.current().delegate = self
                })
// This function has a lot of other code that then calls this function to set multiple notifications :


            for daysInt in outputWeekdays
            {
                scheduleActualNotification(hourInt: hourInt, minuteInt: minuteInt, dayInt: daysInt)
            }
        }

这些是主要功能:

func scheduleActualNotification(hourInt hour: Int, minuteInt minute: Int, dayInt day: Int)
    {
        // 3.1 create date components for each day
        var dateComponents = DateComponents()

        dateComponents.hour = hour
        dateComponents.minute = minute
        dateComponents.day = day

        //3.2 Create a calendar trigger for that day at that time
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents,
                                                    repeats: true)

        //3.3 Message content
        let content = UNMutableNotificationContent()
        content.title = "Test Title"
        content.body = "Test body"
        content.badge = 1

        // 3.4 Create the actual notification
        let request = UNNotificationRequest(identifier: "bob \(i+1)",
                                            content: content,
                                            trigger: trigger)
        // 3.5 Add our notification to the notification center
        UNUserNotificationCenter.current().add(request)
        {
            (error) in
            if let error = error
            {
                print("Uh oh! We had an error: \(error)")
            }
        }

    }

此功能用于在此应用打开时接收通知

This function is to receive the notification when this app is open

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
    completionHandler([.alert, .sound]) //calls the completionHandler indicating that both the alert and the sound is to be presented to the user
}

也没有错误!

因此,从用户界面中,我选择了2350,并选择了星期六和星期日.我希望通知会在周六和周日的2350发送.

So from the user interface, I selected 2350 and Saturday and Sunday. I expect the notification to be sent at 2350 on both Saturday and Sunday.

我做了 print(daysInt).它的第一个值是1,第二个值是7,与预期的一样.其次,我进入了 scheduleActualNotification 函数,与预期的一样,小时的值为23,分钟的值为50.

I did print(daysInt). The first value of it is 1 and the second value of it is 7 which is just as expected. Secondly, I went into the function scheduleActualNotification and the values of hour was 23 and minute was 50, just as expected.

谢谢!

推荐答案

因此,经过数周的挫折,这是解决方案:

So after weeks of frustration, this is the solution :

dateComponents.weekday = day 替换 dateComponents.day = day . .day 是一个月中的一天,而 .weekday 是一个星期中的一天!

Replace dateComponents.day = day with dateComponents.weekday = day. .day is for day of the month whereas .weekday is day of the week!

也不要使用 dateComponents.year = 2017 ,因为这会导致通知无法触发.

Also do not use dateComponents.year = 2017 as that caused the notifications not to fire.

这些通知现在可以正常工作了!

The notifications work perfectly fine now!!

这篇关于Swift 3本地通知未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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