且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-26 21:00:49

经过互联网搜索后,我得到的一件事我们不能编辑 a UILocal通知一旦添加。但确定我找到了另一种方式。

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. 获取设备的所有本地通知。

  2. 搜索相应的本地通知

  3. 取消该通知

  4. 创建新通知

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

以下是添加通知的方法。

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

}

希望你会从中受益。运气***

Hope you will get benefit from it. Best of Luck