大家好,我是目标 c 的新手,我试图在当前时间与名为newarr
我已在该数组中添加时间的数组的时间匹配时触发通知。我希望通知应该根据数组中添加的时间自动触发。现在,只要当前时间与给定数组的时间匹配,就会触发通知,我就做了一个 if 语句。但应用程序工作正常,但只有一个通知被触发。请帮我随后触发通知。
这是我到目前为止所做的:
#import "ViewController.h"
@interface ViewController () {
NSUserDefaults *defaults;
NSString *todaysjustcurrentime;
NSMutableString *todayjusthoursplit;
NSMutableString *todayjustmintsplit;
NSInteger days;
}
@end
bool isGrantedNotificationAccess;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
isGrantedNotificationAccess = false;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert+UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
isGrantedNotificationAccess = granted;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)notifyButton:(id)sender {
if (isGrantedNotificationAccess) {
NSLog(@"clicked");
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Notify me";
content.body = @"Its change of moment in your area";
content.sound = [UNNotificationSound defaultSound];
NSArray *newarr = [[NSArray alloc] initWithObjects:@"18:39",@"18:40",@"18:38",nil];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
// or @"yyyy-MM-dd hh:mm:ss a" if you prefer the time with AM/PM
NSLog(@"here is new current date %@",[dateFormatter stringFromDate:[NSDate date]]);
NSDate* now = [NSDate date];
NSLog(@"here is now date new %@", now);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"HH:mm"];
NSDate *currentDate = [NSDate date];
NSString *dateString = [formatter stringFromDate:currentDate];
NSLog(@"here is the new new date %@", dateString);
NSArray *todayjusttimearray = [dateString componentsSeparatedByString:@":"];
NSLog(@"here is today new new hour split %@", todayjusttimearray);
todayjusthoursplit = todayjusttimearray[0];
todayjustmintsplit = todayjusttimearray[1];
NSLog(@"here is the today new new hour splited new try %@", todayjusthoursplit);
NSLog(@"here is the today today new new mint splited nee try %@", todayjustmintsplit);
NSDateComponents* date = [[NSDateComponents alloc] init];
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"dd-MM-yyyy"];
NSDate *currentdatehere = [NSDate date];
NSString *cureentdatestr = [f stringFromDate:currentdatehere];
NSDate *startDate = [f dateFromString:cureentdatestr];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:startDate];
days = [components day];
date.day = days; //getting current day of the month
date.hour = todayjusthoursplit.intValue;
date.minute = todayjustmintsplit.intValue + 1;
NSLog(@"here is setup date %@", date);
if ([newarr containsObject: dateString])
{
NSLog(@"here is the current time you know ");
}
else {
NSLog(@"here is the current time you dont know ");
}
NSLog(@"this is time right now %@", dateString);
if ([newarr containsObject: dateString])
{
NSLog(@"Yes its time to do something");
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:date repeats:YES];
UNNotificationRequest* request = [UNNotificationRequest
requestWithIdentifier:@"Notify me" content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:nil];
}
else {
NSLog(@"here is the current time does not match ");
}
}
}
@end