是否可以在iPhone中更新本地通知

时间:2012-11-29 07:19:10

标签: objective-c ios5 ios6 xcode4.5 uilocalnotification

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

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

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

2 个答案:

答案 0 :(得分:2)

经过互联网搜索后,我得到了一个我们不能edit添加UILocal通知的东西。但确定我找到了另一种方式。

  1. 获取设备的所有本地通知。
  2. 搜索相应的本地通知
  3. 取消该通知
  4. 创建新的
  5. 以下是添加通知的方法。

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

    希望你能从中受益。最好的运气

答案 1 :(得分:1)

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

eventLocalNotification.fireDate = desiredDate;

对于时区,请按照您的方式保持默认值。

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];

警报正文可以是任何东西,只需记下你的背景。

eventLocalNotification.alertBody = @"DESIRED CONTEXT";