UILocalNotifications在计划时不会触发

时间:2017-02-28 02:50:28

标签: ios swift

嗯,现有的讨论都没有为我提供有效的解决方案。所以我的代码显示了本地通知。我错过了什么?

let notification = UILocalNotification()
notification.alertBody = "Reminder" // text that will be displayed in the notification
notification.alertAction = "open"
notification.applicationIconBadgeNumber = 1
notification.soundName = UILocalNotificationDefaultSoundName


notification.fireDate = NSDate(timeIntervalSinceNow: 5)
print("Notification scheduled")

notification.userInfo = ["title": "Test", "UUID": "1"]
notification.repeatInterval = NSCalendarUnit.WeekOfMonth

UIApplication.sharedApplication().scheduleLocalNotification(notification)

我知道当应用程序位于前台时,didReceiveLocalNotification中应该有一个事件。我没有在appdelegate的didReceiveLocalNotification或通知中收到一个事件。但是,当我将presentLocalNotificationNow与通知一起使用时 - 我会调用app delegate中的didReceiveLocalNotification。我也试过其他的fireDates,但它没有用。我错过了什么?

哦,我的appdelegate didfinishlaunchingapplication中有以下代码

让application = UIApplication.sharedApplication()

let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()

4 个答案:

答案 0 :(得分:2)

我一直在测试,这是我的结果,问题似乎与时间单位weekOfMonth有关,day按预期工作,这是控制台日志的图片,使用XCode 8.2,设备iOS 10.2,仍在工作

enter image description here

func scheduleTestNotification()
{
    let notification = UILocalNotification()
    notification.alertBody = "Reminder" // text that will be displayed in the notification
    notification.alertAction = "open"
    notification.applicationIconBadgeNumber = 1
    notification.soundName = UILocalNotificationDefaultSoundName

    notification.timeZone = NSTimeZone.default
    notification.fireDate = Date(timeIntervalSinceNow: 5)
    print("Notification scheduled")

    notification.userInfo = ["title": "Test", "UUID": "1"]
    notification.repeatInterval = NSCalendar.Unit.day
    UIApplication.shared.scheduleLocalNotification(notification)
    debugPrint(UIApplication.shared.scheduledLocalNotifications!)
}

但是如果我使用weekOfMonth那么

enter image description here

我认为您可以使用此代码作为解决方法,它不是最佳解决方案,但也许可以帮助您实现所需

func scheduleTestNotification()
    {
        let notification = UILocalNotification()
        notification.alertBody = "Reminder" // text that will be displayed in the notification
        notification.alertAction = "open"
        notification.applicationIconBadgeNumber = 1
        notification.soundName = UILocalNotificationDefaultSoundName

        notification.timeZone = NSTimeZone.default
        notification.fireDate = Date(timeIntervalSinceNow: 5)
        print("Notification scheduled")

        notification.userInfo = ["title": "Test", "UUID": "1"]

        UIApplication.shared.scheduleLocalNotification(notification)
        notification.repeatInterval = NSCalendar.Unit.day
        notification.fireDate = Calendar.current.date(byAdding: .day, value: 7, to: notification.fireDate!)
        UIApplication.shared.scheduleLocalNotification(notification)
        debugPrint(UIApplication.shared.scheduledLocalNotifications!)
    }

我希望这有助于你

答案 1 :(得分:1)

要在iOS 10中发送通知,您需要在发出通知之前询问权限。

  func requestPermition() {
        let center = UNUserNotificationCenter.current()
        center.getNotificationSettings { (settings) in
            if settings.authorizationStatus != .authorized {

                let options: UNAuthorizationOptions = [.alert, .sound];
                center.requestAuthorization(options: options) {
                    (granted, error) in
                    if !granted {
                        print("Something went wrong")
                    }else {
                        print("permition granted")
                    }
                }
            }
        }
    }
在iOS 10中不推荐使用{p> UILocalNotification,您可以使用UNUserNotificationCenter代替。发出通知请尝试以下代码

   //to fire notification after 5 seconds.
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5,
                                                    repeats: false)

    //Set contents to be desplayed on the notification
    let content = UNMutableNotificationContent()
    content.title = "Reminder"
    content.body = "Test reminder"
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "myCategory"


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

    //set UNNotificationActionOptions .foreground if you need to open when button tapped.
    let action = UNNotificationAction(identifier: "remindOpen", title: "Open", options: [.foreground])
    let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: [])

    UNUserNotificationCenter.current().setNotificationCategories([category])
    UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("notification error: \(error)")
        }
    }

答案 2 :(得分:0)

我测试了这个代码在我的身边,iOS 10与XCode 8.2

    let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert ], categories: nil)
    UIApplication.shared.registerUserNotificationSettings(settings)

    let notification = UILocalNotification()
    notification.alertBody = "Reminder" // text that will be displayed in the notification
    notification.alertAction = "open"
    notification.applicationIconBadgeNumber = 1
    notification.soundName = UILocalNotificationDefaultSoundName


    notification.fireDate = Date(timeIntervalSinceNow: 5)
    print("Notification scheduled")

    notification.userInfo = ["title": "Test", "UUID": "1"]
    notification.repeatInterval = NSCalendar.Unit.minute

    UIApplication.shared.cancelAllLocalNotifications()
    UIApplication.shared.scheduleLocalNotification(notification)

只要应用程序推送本地通知,就会调用didReceiveNotification方法。我的repeatInterval是分钟而不是weekOfMonth。我认为weekOfMonth是问题,因为当我将repeatInterval更改为weekOfMonth时,我没有收到通知,方法didReceiveNotification从未调用。

答案 3 :(得分:0)

所以我也遇到了同样的问题。 我试图从后台获取中解雇UILocalNotification。我安排了UILocalNotification,我在Xcode中介入,确保调用scheduleLocalNotification,但它只发射了大约1/5次。

似乎从背景提取中调度UINotification在iOS 10中无法可靠地工作。我每次单步执行代码时都切换到使用UNUserNotification并且它可靠地触发。