我创建了一个应用程序,其中应用程序在一天内通知用户一次。
为此,我使用了以下代码
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点通知用户?
答案 0 :(得分:3)
这是因为每次用户打开应用时都会创建新的本地通知。
并每天重复一次通知使用.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默认系统在应用程序的所有代码中都可用,保存到默认系统的任何数据都将通过应用程序会话保留。这意味着即使用户关闭您的应用程序或重新启动他们的手机,下次打开应用程序时保存的数据仍然可用
答案 1 :(得分:2)
创建本地通知时,只需在userInfo有效内容中添加ID即可。 然后,每次打开应用程序时,请检查是否有包含该ID的通知。 如果您找到它然后什么都不做,否则创建一个新的计划。 您不需要在用户默认值中存储任何内容。
-(void) scheduleNotification {
UIApplication *app = [UIApplication sharedApplication];
NSArray *notifications = [app scheduledLocalNotifications];
for (UILocalNotification* notification in notifications)
{
if ([[notification.userInfo objectForKey:@"ID"] isEqualToString:@"MY_NOTIFICATION"])return;
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
notification.alertBody = @"Hey! Upload your latest photos";
notification.userInfo = @{@"ID":@"MY_NOTIFICATION"};
notification.repeatInterval = NSCalendarUnitDay;
notification.soundName = UILocalNotificationDefaultSoundName;
[app scheduleLocalNotification: notification];
}
答案 2 :(得分:1)
操作系统负责在预定时间发送本地通知。 see UILocalNotification
因此操作系统会保持scheduledLocalNotifications计数。
func scheduleNotification() {
if UIApplication.sharedApplication().scheduledLocalNotifications.count == 0 {
let notification = UILocalNotification()
notification.alertBody = "Hey! Update your counter ;)"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate = NSDate()
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
scheduleNotification()
}
答案 3 :(得分:0)
你必须只召唤scheduleNoticaiftion()
一次,所以
试试这个
NSUserDefaults *Check = [NSUserDefaults standardUserDefaults];
if ([Check boolForKey:@"FirstTime"] == NO) {
[Check setBool:YES forKey:@"FirstTime"];
[Check synchronize];
[self scheduleNoticaiftion];
}
答案 4 :(得分:0)
很容易。使用此代码
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
or,
notification.repeatInterval = NSCalendarUnit.CalendarUnitWeekday
并且所有总代码都会像......
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
if( NSUserDefaults.standardUserDefaults().valueForKey("localNotification") == nil )
{
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
NSUserDefaults.standardUserDefaults().setValue("registered", forKey: "localNotification")
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}