Always trigger a notification by matching the current time to that in array in objective c ios
Asked Answered
O

1

0

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
Overthecounter answered 5/4, 2018 at 7:0 Comment(3)
Why compare [NSDate date] with those time in TodayTimeNoteArray? Just loop the value in the TodayTimeNoteArray and set the trigger time?Starstudded
@Starstudded how can I know when to trigger next at what time so I am doing plz give an answer as I want thank in advanceOverthecounter
I am also getting these type conversion warnings am researching that but could not found any helpOverthecounter
S
1

For your logic, Im not sure what you are trying to do, so I wont talk about that. To fix the warning, simply change your code a little bit as below then you will get correct date when print:

if ([TodayTimeNoteArray containsObject: todaysjustcurrentime])
{

    date.day = days; //getting current day of the month
    date.hour = todayjusthoursplit.intValue;
    date.minute = todayjustmintsplit.intValue;
Starstudded answered 5/4, 2018 at 7:45 Comment(4)
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array'Overthecounter
Print all your array and make sure they are not nilStarstudded
array is not being printed however I can print its subscript [0] or 1 2 3Overthecounter
Now every thing is ok array has timings array is not nil app is not being crashed but notification does not triggerOverthecounter

© 2022 - 2024 — McMap. All rights reserved.