【问题标题】:I want to set different alert body for local notification in ios app using objective c我想使用objective c在ios应用程序中为本地通知设置不同的警报正文
【发布时间】:2016-04-16 17:01:21
【问题描述】:

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

这是我的警报正文 :---(@"Ask %@ How you doin, How you been?",name)

这是我的代码,

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

【问题讨论】:

  • 现在发生了什么?
  • 顺便说一句,下面这行令人憎恶:if ([[[self.localContactArray objectAtIndex:randomIndex] objectForKey:@"lName"] isEqualToString:@"&lt;null&gt;"] || [[[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){

标签: ios objective-c uilocalnotification


【解决方案1】:

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

【讨论】:

  • 但请帮助某人在本地通知中显示不同的警报正文
  • 什么时候调用 setNotificationlocal 方法?使用您的代码,我认为每次调用时 alertBody 都不同,因为名称来自随机人。
  • 当我当时选择通知时间时使用此方法
  • 如何为每个通知选择不同的联系人姓名
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多