Search code examples
iosobjective-cnotificationsuilocalnotification

How to get particular notification count number


I have three categories of UILocalNotification - every Sunday, Monday and Saturday Notifications.

How to get the particular notification i.e Monday triggered count number. I have a notification UILocalNotification (not remote or push notification).

How do I get count of Monday Notification which are triggered within two Months?

Note: There is no scope of push or remote notifications.


Solution

  • You'd have to manually manage this, the way I would suggest doing it is by utilising the didReceiveLocalNotification method of your AppDelegate. Implement this and then check the fireDate property of your UILocalNotificationObject. You can parse this to be a day of the week by using the following code:

    int dayOfWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:notification.fireDate];
    

    You can then store increment this value in the NSUserDefaults based on the number you have. 1 will be Monday, 2 Tuesday etc...

    long dayOfWeek = [[NSCalendar currentCalendar] component:NSCalendarUnitWeekday fromDate:notification.fireDate];
    long currentCount = [[NSUserDefaults standardUserDefaults] integerForKey:[NSString stringWithFormat:@"weekdayNotificationCount_%ld", dayOfWeek]];
    [[NSUserDefaults standardUserDefaults] setInteger:currentCount + 1 forKey:[NSString stringWithFormat:@"weekdayNotificationCount_%ld", dayOfWeek]];
    

    That way you will always have a running total stored in your NSUserDefaults and this will only disappear if the app is deleted.