ios - 如何一天只重复一次 LocalNotification

标签 ios swift nsnotificationcenter localnotification

我创建了一个应用程序,该应用程序每天通知用户一次。

为此我使用了下面的代码

 func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

    scheduleNoticaiftion()
}

// Schedule the Notifications with repeat
func scheduleNoticaiftion() {
    //UIApplication.sharedApplication().cancelAllLocalNotifications()

    // Schedule the notification ********************************************

        let notification = UILocalNotification()
        notification.alertBody = "Hey! Upload your latest photos"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.fireDate = NSDate()
       // notification.category = categoryID
        notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

        UIApplication.sharedApplication().scheduleLocalNotification(notification)

}

上面的代码工作正常,但问题是如果用户一天打开和关闭(终止)应用程序 6 次。

根据应用程序打开时间,用户在第二天总共收到 6 条通知,即如果我在下午 3 点打开应用程序并关闭它,然后在下午 4 点打开下一个应用程序。它将在下午 3 点和次日下午 4 点同时显示通知。

问题:如何在 24 小时内只发送一个通知?即如果用户在下午 4 点安装我的应用程序,它会在每天下午 4 点通知用户吗?

最佳答案

这是因为每次用户打开应用时您都会创建新的本地通知。

并每天使用 .CalendarUnitDay 重复通知一次

试试下面的代码

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        if(!NSUserDefaults.standardUserDefaults() .boolForKey("isNotificationScheduled")){
            scheduleNoticaiftion()
        }
        return true
    }

    // Schedule the Notifications with repeat
    func scheduleNoticaiftion() {
        //UIApplication.sharedApplication().cancelAllLocalNotifications()

        // Schedule the notification ********************************************

        let notification = UILocalNotification()
        notification.alertBody = "Hey! Upload your latest photos"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.fireDate = NSDate()
        // notification.category = categoryID
        notification.repeatInterval = NSCalendarUnit.CalendarUnitDay

        UIApplication.sharedApplication().scheduleLocalNotification(notification)

        NSUserDefaults .standardUserDefaults() .setBool(true, forKey: "isNotificationScheduled")
        NSUserDefaults .standardUserDefaults() .synchronize()
    }

NSUserDefaults 类,您可以保存与应用程序或用户数据相关的设置和属性。例如,您可以保存用户设置的个人资料图像或应用程序的默认配色方案。这些对象将保存在所谓的 iOS“默认系统”中。 iOS 默认系​​统在应用程序的所有代码中都可用,保存到默认系统的任何数据都将在应用程序 session 中持续存在。这意味着即使用户关闭您的应用程序或重新启动他们的手机,保存的数据在他们下次打开应用程序时仍然可用

关于ios - 如何一天只重复一次 LocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30096668/

相关文章:

macos - 在swift中添加通知观察者

iOS:如何正确停止事件指示器?

ios - Swift 中的 NSNotificationCenter 行为不正确(只调用函数中的某些东西)

ios - 如何在 Swift 中将 xib 的垂直长度设置为代码

swift - iOS 10 消息扩展应用程序 : How can I get the MSSticker created by this function?

python - Ruby 或 Python 是否因为速度而不适合 iPhone 或 Mac 应用程序开发,以及任何可以帮助提高速度的编译器?

ios - 以 UILabel 文本的第一行居中图像

ios - 没有箭头的 UIPopoverPresentation

iOS swift : Cannot Manipulate String: Value of type 'String' has no member 'firstIndex'

ios - 刷新持有 UITableView 的容器 View - Swift