我想使用目标c在ios app中为本地通知设置不同的警报正文

时间:2016-01-12 07:16:21

标签: ios objective-c uilocalnotification

我正在从地址簿中获取联系人。通过为每个通知获取不同的联系人姓名来显示警报正文,但它不起作用。

这是我警觉的身体:---(@“问%@你怎么做,你怎么样?”,姓名)

这是我的代码,

-(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];
    }
}

1 个答案:

答案 0 :(得分:0)

As this answer,您只需要检查

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"]];