是否有可能以更新iPhone本地通知(Is it possible to update the lo

2019-08-03 07:47发布

我已编程方式添加的本地通知如下图所示:

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

我可以改变firingDate,时区alertBody,或任何其他财产?

Answer 1:

在互联网搜索了很多之后,我得到了,我们不能一件事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;
        }
    }

}

希望大家能够从中获益。 祝您好运



Answer 2:

要设置日期和时间,你将不得不使用NSDateComponents和实例的NSDate两次。 一为当前日期和另一个将是您理想的日期。 比预期的NSDate的实例设置为UILocalNotification的对象fireDate。

eventLocalNotification.fireDate = desiredDate;

对时区,保持默认你的方式。

eventLocalNotification.timeZone = [NSTimeZone defaultTimeZone];

警报体可以是任何东西,只要把你的背景。

eventLocalNotification.alertBody = @"DESIRED CONTEXT";


文章来源: Is it possible to update the local notification in iPhone