Hello guys I am new to objective c, I am trying to trigger a notification when the current time matches
the time of an array named newarr
i have added times in this array. I want the notifications should be triggered automatically based on the times added in array. Now I have made an if statement whenever the current time matches the time given an array the notification is being triggered. but the ap is working fine but the only one notification is being triggered. please help me to trigger the notification subsequently.
Here is what I have done so far:
#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