Reputation: 2904
I create a application in which application notify the users once in a day.
For that I used the following code
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)
}
The above code is working fine but the problem is if user opens and close(terminate) the app 6 time in a day.
User get total 6 notification on next day based on application open time i.e. if I open application in 3 p.m and close it and next open application at 4 p.m. it will show the notification in 3pm and 4pm of next day simultaneously.
Question: How can I send only one notification within 24 hours? i.e. If a user install my app at 4pm it will notify the user every day at 4pm ?
Upvotes: 0
Views: 1738
Reputation: 2904
The operating system is responsible for delivering local notifications at their scheduled times. see UILocalNotification
So operating system maintain the scheduledLocalNotifications count.
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()
}
Upvotes: 1
Reputation: 201
Simply add an ID to the userInfo payload when you create the local notification. Then every time you open the app check if is there a notification with that ID. If you find it then do nothing, otherwise create a new schedule. You won't need to store anything in user defaults.
-(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];
}
Upvotes: 2
Reputation: 2848
It's because you create new local notification each time user opens the app.
and to repeat the notification once per day use .CalendarUnitDay
try below code
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, you can save settings and properties related to application or user data. For example, you could save a profile image set by the user or a default color scheme for the application. The objects will be saved in what is known as the iOS “defaults system”. The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions. This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app
Upvotes: 4
Reputation: 165
It is easy. Use this code
notification.repeatInterval = NSCalendarUnit.CalendarUnitDay
or,
notification.repeatInterval = NSCalendarUnit.CalendarUnitWeekday
and All total code wiil be like...
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)
}
Upvotes: 0
Reputation: 643
you have to call scheduleNoticaiftion()
only one time, so
Try this
NSUserDefaults *Check = [NSUserDefaults standardUserDefaults];
if ([Check boolForKey:@"FirstTime"] == NO) {
[Check setBool:YES forKey:@"FirstTime"];
[Check synchronize];
[self scheduleNoticaiftion];
}
Upvotes: 0