-1

I am fetching contacts from adress book & show the alert body by fetching different contact name for every notification but it is not working.

This is my alert body :---(@"Ask %@ How you doin, How you been?",name)

here is my code,

-(void)setNotificationlocal{

    //get random contacts from adress book
   NSUInteger randomIndex = arc4random() % [self.localContactArray count];
    NSLog(@"random index---%lu",(unsigned long)randomIndex);
    NSString *name;
    NSString *mobNumber;
    if ([[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isEqualToString:@"<null>"] || [[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isEqualToString:@""] || [[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"]isEqualToString:@"(null)"]||[[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"]  isKindOfClass:[NSNull class]]  || [[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] == nil){

        name=[[NSString alloc]initWithFormat:@"%@",[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"fName"]];
        mobNumber=nil;
        mobNumber=[[NSString alloc]initWithString:[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"num"]];
    }else{

        name=[[NSString alloc]initWithFormat:@"%@ %@",[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"fName"],[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"]];
        mobNumber=nil;
        mobNumber=[[NSString alloc]initWithString:[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"num"]];
    }


    // New for iOS 8 - Register the notifications

    UILocalNotification *notification = [[UILocalNotification alloc] init];
    if (notification)
    {
            notification.timeZone = [NSTimeZone defaultTimeZone];
            if ( [statusString isEqualToString:@"daily"]) {

                notification.fireDate =[NSDate dateWithTimeIntervalSinceNow:86400];
               // notification.repeatInterval = NSCalendarUnitDay;
               NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:@"daily" forKey:@"KEY_NAME"];
                [defaults synchronize];

            }else if ( [statusString isEqualToString:@"weekly"]) {

                notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:604800];
               // notification.repeatInterval = NSCalendarUnitWeekOfYear;
               NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:@"weekly" forKey:@"KEY_NAME"];
                [defaults synchronize];

            }else if ( [statusString isEqualToString:@"fortnightly"]) {
                notification.fireDate =  [NSDate dateWithTimeIntervalSinceNow:1209600];

//                notification.userInfo = @{@"notification_identifier":@"After14Days"};
//                notification.fireDate = [[NSDate date] dateByAddingTimeInterval:(60*60*24*14)];
//                notification.repeatInterval = NSCalendarUnitDay;
               NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:@"fortnightly" forKey:@"KEY_NAME"];
                [defaults synchronize];

            }else{
                notification.repeatInterval = 0;
            }

            notification.alertBody = [NSString stringWithFormat:@"Ask %@ How you doin, How you been?",name];
            notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
            notification.soundName = UILocalNotificationDefaultSoundName;

        notification.alertAction = @"say yes";
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
       // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSMS) name:@"showSMS" object:nil];

        // this will schedule the notification to fire at the fire date
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}
Parth Pandya
  • 1,460
  • 3
  • 18
  • 34
  • What is happening now? – Anoop Vaidya Jan 12 '16 at 07:18
  • 1
    As an aside, the following line is an abomination: `if ([[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isEqualToString:@""] || [[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isEqualToString:@""] || [[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"]isEqualToString:@"(null)"]||[[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isKindOfClass:[NSNull class]] || [[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] == nil){` – Avi Jan 12 '16 at 07:40

1 Answers1

0

As this answer, you just need to check

if ([[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] == nil)
{
    name=[[NSString alloc]initWithFormat:@"%@",[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"fName"]];
}
else
{
    name=[[NSString alloc]initWithFormat:@"%@ %@",[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"fName"],[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"]];
}
    mobNumber=[[NSString alloc]initWithString:[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"num"]];
Community
  • 1
  • 1
Vũ Ngọc Giang
  • 234
  • 3
  • 12