3

我以编程方式添加了一个本地通知,如下所示:

UILocalNotification *eventLocalNotification=[[UILocalNotification alloc]init];
eventLocalNotification.fireDate=myDate;
eventLocalNotification.timeZone=[NSTimeZone defaultTimeZone];
eventLocalNotification.alertBody=@"My notification";
eventLocalNotification.soundName=UILocalNotificationDefaultSoundName;

我可以更改 fireDate、timeZone、alertBody 或任何其他属性吗?

4

2 回答 2

2

在互联网上搜索了很多之后,我得到了一个我们无法edit添加 UILocal 通知的东西。但可以肯定的是,我发现了另一种方法。

  1. 获取您设备的所有本地通知。
  2. 搜索相应的本地通知
  3. 取消该通知
  4. 创建一个新的

下面是添加通知的方法。

-(void)setLocalNotification
{
    UILocalNotification *eventLocalNotification =   [[UILocalNotification alloc]init];

    eventLocalNotification.fireDate     =   //set firing Date of NSDate type
    eventLocalNotification.timeZone     =   [NSTimeZone defaultTimeZone];
    eventLocalNotification.alertBody    =   [NSString stringWithFormat:@"An event has arrived\n Event Name: %@",notificationName.Text];
    eventLocalNotification.soundName    =   UILocalNotificationDefaultSoundName;

    if ([repeat isEqualToString:@"Once"]){

        eventLocalNotification.repeatInterval   =   0;

    }else if ([repeat isEqualToString:@"Daily"]){

        eventLocalNotification.repeatInterval   =   NSDayCalendarUnit;

    }else if ([repeat isEqualToString:@"Weekly"]){

        eventLocalNotification.repeatInterval   =   NSWeekCalendarUnit;

    }else if ([repeat isEqualToString:@"Monthly"]){

        eventLocalNotification.repeatInterval   =   NSMonthCalendarUnit;

    }else if ([repeat isEqualToString:@"Yearly"]){

        eventLocalNotification.repeatInterval   =   NSYearCalendarUnit;
    }

    NSDictionary *info  =   [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@",notificationName.text] forKey:@"name"];
    eventLocalNotification.userInfo =   info;
    NSLog(@"notification userInfo gets name : %@",[info objectForKey:@"name"]);
    [[UIApplication sharedApplication] scheduleLocalNotification:eventLocalNotification];
    NSLog(@"Notification created");
}

下面是取消通知的功能

-(void)cancelLocalNotification
{
    UILocalNotification * notificationToCancel=nil;
    for(UILocalNotification * aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications])
    {
        NSLog(@"%@",[aNotif.userInfo objectForKey:@"name"]);
        if([[aNotif.userInfo objectForKey:@"name"] isEqualToString:notificationName ])
        {
            notificationToCancel    =   aNotif;
            [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];
            NSLog(@"Notification Cancelled");
            break;
        }
    }

}

希望您能从中受益。祝你好运

于 2012-12-26T12:46:24.983 回答
1

要设置日期和时间,您必须使用 NSDateComponents 并实例化 NSDate 两次。一个用于当前日期,另一个将是您想要的日期。将所需的 NSDate 实例设置为 UILocalNotification 对象的 fireDate。

eventLocalNotification.fireDate = desiredDate;

对于时区,将其保持为默认设置。

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];

警报主体可以是任何东西,只需输入您的上下文即可。

eventLocalNotification.alertBody = @"DESIRED CONTEXT";
于 2012-11-29T08:24:11.060 回答