2012-11-29 37 views
3

我添加了一個本地通知編程象下面這樣:是否可以更新本地通知在iPhone

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

我可以更改firingDate,時區alertBody,或任何其他財產?

+4

可能的重複http://stackoverflow.com/questions/3842252/is-there-a-simple-way-to-edit-modify-a-uilocalnotification – John

+0

是的!但不幸的是我不能接觸到這個問題。並且它對我也沒什麼幫助。 – iOmi

回答

1

要設置您的日期和時間,您將不得不使用NSDateComponents並實例化兩次NSDate。一個當前日期和另一個將是您的理想日期。比將理想的NSDate實例設置爲UILocalNotification對象的fireDate。

eventLocalNotification.fireDate = desiredDate; 

對於時區,請按照您的方式保持默認。

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone]; 

警報機構可以是任何東西,只要把你的上下文。

eventLocalNotification.alertBody = @"DESIRED CONTEXT"; 
+0

對不起兄弟,這實際上並不是我想要的 – iOmi

+0

你到底在找什麼? –

+0

我想我的問題很清楚,無論如何,我已經添加了一個通知。現在我想編輯它。 – iOmi

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

} 

希望大家能夠從中得到好處的功能。祝你好運

+0

太棒了,你幫了我很多 – lenhhoxung