问题描述
我正在做一个祈祷应用程序,使用户能够为祈祷时间设置警报(本地通知),即用户设置应用程序每天通知他Fajr祷告,问题是每个祷告的时间每天更改所以应用程序将在周四通知用户公平的时间将与星期五的时间不同,我需要每天重复本地通知,但是根据每日祷告时间,请问,有人可以给我一个想法吗?
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?
推荐答案
有一些可能的解决方案。使用一次安排有限数量的通知的方法可能更安全,因为iOS仅保留64个最快的通知:
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:
来源:UILocalNotification类引用
依靠使用 UILocalNotification
传入应用程序:didFinishLaunchingWithOptions:
,因为它仅在用户滑动通知时传递:
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:
密钥响应本地通知而启动的值为:
UIApplicationLaunchOptionsLocalNotificationKey
The key value for launching in response to a local notification is:UIApplicationLaunchOptionsLocalNotificationKey
来源:UIApplicationDelegate课程参考
选项1:一次安排一天(以下提供此代码)
处理通知计划的一种方法是向用户提供计划,其中当天的通知是在应用程序首次打开时安排的。
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.
使用 CustomNotificationManager
类来处理时间可变的通知(下面提供的代码)。在您的AppDelegate中,您可以委派此课程处理本地通知,该通知将安排当天的通知加上第二天的固定时间通知,或者回复祷告通知。
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.
选项2(稍微瘦一些的方法,但对用户提供的更少)
另一种方法是简单地使用祷告通知的应用程序启动来安排立即如下。但是,这不太可靠,并且无法预览通知计划。
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.
通知管理器标头文件
@interface CustomNotificationManager : NSObject
- (void) handleLocalNotification:(UILocalNotification *localNotification);
@end
通知管理器实施文件
#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];
}
建议可能的修改:如果CustomNotificationManager需要为了维持状态,你可以将其转换为单身人士。
Suggestion for possible modification: If the CustomNotificationManager needs to maintain state, you could convert it to a Singleton.
这篇关于如何在不同时间每天重复本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!