Is it possible to update the local notification in iPhone

爱⌒轻易说出口 提交于 2019-12-21 20:25:13

问题


I have added a local notification programmatically like below:

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

Can I change the firingDate, timeZone, alertBody, or any other property?


回答1:


After searching a lot over internet, I got a thing that we can't edit a UILocal Notification once added. But sure there is another way that I have found.

  1. Get all the Local notification of your device.
  2. Search the respective local notification
  3. Cancel that notification
  4. Create a New one

Below is the method to add the notification.

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

Below is the function to cancel the notification

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

}

Hope you will get benefit from it. Best of Luck




回答2:


To set your date and time, you will have to use NSDateComponents and instantiate NSDate twice. One for current date and another will be your desirable date. Than set desirable NSDate's instance to fireDate of UILocalNotification's object.

eventLocalNotification.fireDate = desiredDate;

for timezone, keep it to default the way you did.

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];

Alert body could be anything, just put your context.

eventLocalNotification.alertBody = @"DESIRED CONTEXT";


来源:https://stackoverflow.com/questions/13620689/is-it-possible-to-update-the-local-notification-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!