如何一天只重复一次 LocalNotification
How to repeat the LocalNotification only once in a day
我创建了一个每天通知用户一次的应用程序。
为此,我使用了以下代码
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 p.m 打开应用程序并关闭它,下一个在 4 p.m 打开应用程序。它将在下午 3 点和次日下午 4 点同时显示通知。
问题:如何在24小时内只发送一个通知?即如果用户在下午 4 点安装我的应用程序,它会在每天下午 4 点通知用户吗?
你只需调用 scheduleNoticaiftion()
一次,所以
试试这个
NSUserDefaults *Check = [NSUserDefaults standardUserDefaults];
if ([Check boolForKey:@"FirstTime"] == NO) {
[Check setBool:YES forKey:@"FirstTime"];
[Check synchronize];
[self scheduleNoticaiftion];
}
这是因为每次用户打开应用程序时您都会创建新的本地通知。
并每天重复一次通知,使用 .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 class,您可以保存与应用程序或用户数据相关的设置和属性。例如,您可以保存用户设置的个人资料图像或应用程序的默认配色方案。这些对象将保存在所谓的 iOS “默认系统”中。 iOS 默认系统在应用程序的所有代码中都可用,保存到默认系统的任何数据都将在应用程序会话中持续存在。这意味着即使用户关闭您的应用程序或重新启动他们的 phone,保存的数据在他们下次打开应用程序时仍然可用
创建本地通知时,只需将 ID 添加到 userInfo 负载即可。
然后每次打开应用程序时检查是否有带有该 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];
}
很简单。使用此代码
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)
}
操作系统负责在预定时间发送本地通知。 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()
}
我创建了一个每天通知用户一次的应用程序。
为此,我使用了以下代码
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 p.m 打开应用程序并关闭它,下一个在 4 p.m 打开应用程序。它将在下午 3 点和次日下午 4 点同时显示通知。
问题:如何在24小时内只发送一个通知?即如果用户在下午 4 点安装我的应用程序,它会在每天下午 4 点通知用户吗?
你只需调用 scheduleNoticaiftion()
一次,所以
试试这个
NSUserDefaults *Check = [NSUserDefaults standardUserDefaults];
if ([Check boolForKey:@"FirstTime"] == NO) {
[Check setBool:YES forKey:@"FirstTime"];
[Check synchronize];
[self scheduleNoticaiftion];
}
这是因为每次用户打开应用程序时您都会创建新的本地通知。
并每天重复一次通知,使用 .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 class,您可以保存与应用程序或用户数据相关的设置和属性。例如,您可以保存用户设置的个人资料图像或应用程序的默认配色方案。这些对象将保存在所谓的 iOS “默认系统”中。 iOS 默认系统在应用程序的所有代码中都可用,保存到默认系统的任何数据都将在应用程序会话中持续存在。这意味着即使用户关闭您的应用程序或重新启动他们的 phone,保存的数据在他们下次打开应用程序时仍然可用
创建本地通知时,只需将 ID 添加到 userInfo 负载即可。 然后每次打开应用程序时检查是否有带有该 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];
}
很简单。使用此代码
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)
}
操作系统负责在预定时间发送本地通知。 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()
}