Swift 3 Local notifications not firing

独自空忆成欢 提交于 2020-01-04 02:40:12

问题


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)
            }
        }

And these are the main function :

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
}

There are no errors either lol!

Edit :

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.

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.

Thank you!


回答1:


So after weeks of frustration, this is the solution :

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

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

The notifications work perfectly fine now!!




回答2:


Calendar Unit Flags can help you :

var notificationTriggerCalendar : UNCalendarNotificationTrigger? = nil

if let fireDate = trigger.remindAt {

  let unitFlags = Set<Calendar.Component>([.hour, .year, .minute, .day, .month, .second])
  var calendar = Calendar.current
  calendar.timeZone = .current
  let components = calendar.dateComponents(unitFlags, from: fireDate)
  let newDate = Calendar.current.date(from: components)

  notificationTriggerCalendar = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
}


来源:https://stackoverflow.com/questions/45259189/swift-3-local-notifications-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!