Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat local notifications every day at different times

I'm working on a prayers application that enables users to set an alarm(local notification) for prayer times, i.e. the user sets the application to notify him for Fajr prayer every day, the problem is that the time for each prayer changes daily so the time the app will notify the user for fair in thursday will differ from the time in friday, i need to repeat the local notification every day but according to the daily prayer time, please, could anyone give me an idea?

like image 352
Eslam Mohamed Mohamed Avatar asked Sep 06 '14 13:09

Eslam Mohamed Mohamed


People also ask

How do I send a local alert with a repeat interval?

In models, we need to create the below class with Title, Body, ID, IconId and NotifyTime. It will be useful to pass a local notification data to BroadcastReceiver receiver for repeating in Android. Create a class LocalNotificationService and we need to implement LocalNotification and Cancel methods like below.

How many local notifications can be scheduled iOS?

Local notification volume is limited to 64 notifications per iOS app per day, which can be constraining if notifications are vital to your app's functionality and you have a large user base.


2 Answers

There are a few possible solutions for this. It might be safer to use an approach where a limited number of notifications are scheduled at a time, since iOS only keeps the 64 soonest notifications:

An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest.

Source: UILocalNotification class reference

It is also not a good idea to rely on using the UILocalNotification passed into application:didFinishLaunchingWithOptions:, since it is only passed when the user swipes the notification:

Look at the launch options dictionary to determine why your app was launched. The application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: methods provide a dictionary with keys indicating the reason that your app was launched.

The key value for launching in response to a local notification is: UIApplicationLaunchOptionsLocalNotificationKey

Source: UIApplicationDelegate class reference

Option 1: schedule one day at a time (Code for this is provided below)

One way to handle the scheduling of notifications is to present a schedule to the user, where the day's notifications are scheduled at the time of the initial opening of the app.

Use a CustomNotificationManager class to handle notifications whose times are variable (code provided below). In your AppDelegate, you can delegate to this class the handling of the local notifications, which will either schedule the current day's notifications plus the following day's fixed-time notification, or respond to a prayer notification.

If the User opens the app in response to a prayer notification, the app can direct the user to an appropriate part of the app. If the user opens the app in response to the fixed-time notification, the app will schedule that day's local notifications according to the User's date and location.

Option 2 (Slightly slimmer approach, but which provides less to the User)

Another approach is to simply use a prayer notification's app launch to schedule the one that immediately follows. However, this is less reliable, and does not provide the ability to preview a schedule of notifications.

Notification Manager Header file

@interface CustomNotificationManager : NSObject

- (void) handleLocalNotification:(UILocalNotification *localNotification);

@end

Notification Manager Implementation file

#import "CustomNotificationManager.h"

#define CustomNotificationManager_FirstNotification @"firstNotification"

@implementation CustomNotificationManager

- (instancetype) init
{
    self = [super init];

    if (self) {

    }

    return self;
}

- (void) handleLocalNotification:(UILocalNotification *)localNotification
{
    //Determine if this is the notification received at a fixed time,
    //  used to trigger the scheculing of today's notifications
    NSDictionary *notificationDict = [localNotification userInfo];
    if (notificationDict[CustomNotificationManager_FirstNotification]) {
        //TODO: use custom algorithm to create notification times, using today's date and location
        //Replace this line with use of algorithm
        NSArray *notificationTimes = [NSArray new];

        [self scheduleLocalNotifications:notificationTimes];
    } else {
        //Handle a prayer notification
    }

}

/**
 * Schedule local notifications for each time in the notificationTimes array.
 *
 * notificationTimes must be an array of NSTimeInterval values, set as intervalas
 * since 1970.
 */
- (void) scheduleLocalNotifications:(NSArray *)notificationTimes
{
    for (NSNumber *notificationTime in notificationTimes) {
        //Optional: create the user info for this notification
        NSDictionary *userInfo = @{};

        //Create the local notification
        UILocalNotification *localNotification = [self createLocalNotificationWithFireTimeInterval:notificationTime
                                                                                       alertAction:@"View"
                                                                                         alertBody:@"It is time for your next prayer."
                                                                                          userInfo:userInfo];

        //Schedule the notification on the device
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

    /* Schedule a notification for the following day, to come before all other notifications.
     *
     * This notification will trigger the app to schedule notifications, when
     * the app is opened.
     */

    //Set a flag in the user info, to set a flag to let the app know that it needs to schedule notifications
    NSDictionary *userInfo = @{ CustomNotificationManager_FirstNotification : @1 };

    NSNumber *firstNotificationTimeInterval = [self firstNotificationTimeInterval];

    UILocalNotification *firstNotification = [self createLocalNotificationWithFireTimeInterval:firstNotificationTimeInterval
                                                                                   alertAction:@"View"
                                                                                     alertBody:@"View your prayer times for today."
                                                                                      userInfo:userInfo];

    //Schedule the notification on the device
    [[UIApplication sharedApplication] scheduleLocalNotification:firstNotification];
}

- (UILocalNotification *) createLocalNotificationWithFireTimeInterval:(NSNumber *)fireTimeInterval
                                                    alertAction:(NSString *)alertAction
                                                    alertBody:(NSString *)alertBody
                                                     userInfo:(NSDictionary *)userInfo

{
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (!localNotification) {
        NSLog(@"Could not create a local notification.");
        return nil;
    }

    //Set the delivery date and time of the notification
    long long notificationTime = [fireTimeInterval longLongValue];
    NSDate *notificationDate = [NSDate dateWithTimeIntervalSince1970:notificationTime];
    localNotification.fireDate = notificationDate;

    //Set the slider button text
    localNotification.alertAction = alertAction;

    //Set the alert body of the notification
    localNotification.alertBody = alertBody;

    //Set any userInfo, e.g. userID etc. (Useful for app with multi-user signin)
    //The userInfo is read in the AppDelegate, via application:didReceiveLocalNotification:
    localNotification.userInfo = userInfo;

    //Set the timezone, to allow for adjustment for when the user is traveling
    localNotification.timeZone = [NSTimeZone localTimeZone];

    return localNotification;
}

/**
 * Calculate and return a number with an NSTimeInterval for the fixed daily
 * notification time.
 */
- (NSNumber *) firstNotificationTimeInterval
{
    //Create a Gregorian calendar
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    //Date components for next day
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    dateComps.day = 1;

    //Get a date for tomorrow, same time
    NSDate *today = [NSDate date];
    NSDate *tomorrow = [cal dateByAddingComponents:dateComps toDate:today options:0];

    //Date components for the date elements to be preserved, when we change the hour
    NSDateComponents *preservedComps = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:tomorrow];
    preservedComps.hour = 5;
    tomorrow = [cal dateFromComponents:preservedComps];

    NSTimeInterval notificationTimeInterval = [tomorrow timeIntervalSince1970];

    NSNumber *notificationTimeIntervalNum = [NSNumber numberWithLongLong:notificationTimeInterval];

    return notificationTimeIntervalNum;
}

@end

AppDelegate didReceiveLocalNotification Implementation

- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    CustomNotificationManager *notificationManager = [[CustomNotificationManager alloc] init];
    [notificationManager handleLocalNotification:notification];
}

Suggestion for possible modification: If the CustomNotificationManager needs to maintain state, you could convert it to a Singleton.

like image 86
Sheamus Avatar answered Sep 28 '22 03:09

Sheamus


So the problem appears you need to be setting this local notification every now and then, but can't be a repeatable notification. I assume the user sets the prayer time, and wants to be notified. I suggest you set a few of them, since you know from the list. Then set background fetch for let say every 5 hours, and upon app background launch, just check what local notifications are still set, and update the list accordingly based on the current date. Background fetch doesn't wake your app precisely every 5 hours in this case, but will do its best. I'm sure your app will wake up at least twice a day. You can tweak the time based on your needs.

Fetching Small Amounts of Content Opportunistically Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content. To support this mode, enable the Background fetch option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the fetch value in your app’s Info.plist file.) Enabling this mode is not a guarantee that the system will give your app any time to perform background fetches. The system must balance your app’s need to fetch content with the needs of other apps and the system itself. After assessing that information, the system gives time to apps when there are good opportunities to do so. When a good opportunity arises, the system wakes or launches your app into the background and calls the app delegate’s application:performFetchWithCompletionHandler: method. Use that method to check for new content and initiate a download operation if content is available. As soon as you finish downloading the new content, you must execute the provided completion handler block, passing a result that indicates whether content was available. Executing this block tells the system that it can move your app back to the suspended state and evaluate its power usage. Apps that download small amounts of content quickly, and accurately reflect when they had content available to download, are more likely to receive execution time in the future than apps that take a long time to download their content or that claim content was available but then do not download anything.

For more information refers to Apple's documentation on background execution:

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

like image 35
carlos Avatar answered Sep 28 '22 03:09

carlos