我如何在UILocalNotification中更改UserInfo?

时间:2013-04-10 11:34:22

标签: iphone ios objective-c uilocalnotification

我有6个 UILocalNotifications。 每个通知都有自己的“userInfo”

  1. @ “1”
  2. @ “2”
  3. @ “3”
  4. @ “4”
  5. @ “5”
  6. @ “6”
  7. 然后我删除通知编号4,并有5个通知:

    1. @ “1”
    2. @ “2”
    3. @ “3”
    4. @ “5”
    5. @ “6”
    6. 如何在通知中更改“userInfo”以获取:

      1. @ “1”
      2. @ “2”
      3. @ “3”
      4. @ “4”
      5. @ “5”
      6. 保存代码 -

        // Specify custom data for the notification
        
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%i", i] forKey:@"notif"];
        localNotif.userInfo = infoDict;
         i = i + 1;
        

        Del code -

        if (flagTable == 1)
        {
            int numberPhoto = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
            int numPhFlag = numberPhoto;
            int i = 0;
            NSLog(@"Col notif = %d", numberPhoto);
            UILocalNotification *localNotif = [[UILocalNotification alloc] init];
            while (numberPhoto == numPhFlag) {
                localNotif = [[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i];
                localNotif.timeZone = [NSTimeZone localTimeZone];
                NSDateFormatter *format = [[NSDateFormatter alloc] init];
                [format setDateFormat:@"dd-MM-yyyy HH:mm"];
                NSString *itemDateWithFormat = [format stringFromDate:localNotif.fireDate];
                NSLog(@"remText2 %@ = fireDate2 %@", itemDateWithFormat, [appDel.fireDateArray objectAtIndex:row]);
                if ([itemDateWithFormat isEqualToString:[appDel.fireDateArray objectAtIndex:row]]) {
                    NSLog(@"remText3 %@ = fireDate3 %@", itemDateWithFormat, [appDel.fireDateArray objectAtIndex:row]);
                    [[UIApplication sharedApplication] cancelLocalNotification:localNotif];
                    numPhFlag = numPhFlag - 1;
                }
                i = i + 1;
            }
            int getNot = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
            NSLog(@"Col notif2 = %d", getNot);
        }
        

        我想迭代通知并替换其userInfo中的数据。

2 个答案:

答案 0 :(得分:5)

得到答案=)

int notifCount = [[[UIApplication sharedApplication] scheduledLocalNotifications] count];
for (int j = 0; j < notifCount; j++) {
    UILocalNotification *notific = [[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:j];
    NSLog(@"notificBefore = %@ and j = %i", [notific.userInfo objectForKey:@"notif"], j+1);
    if ([notific.userInfo objectForKey:@"notif"] != [NSString stringWithFormat:@"%i", j+1]) {
        [[UIApplication sharedApplication] cancelLocalNotification:notific];
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%i", j+1] forKey:@"notif"];
        [notific setUserInfo:infoDict];
        NSLog(@"notificEnd = %@", [notific.userInfo objectForKey:@"notif"]);
        [[UIApplication sharedApplication] scheduleLocalNotification:notific];
    }
}

答案 1 :(得分:1)

关于UILocalNotification的一件事是您无法编辑任何uilocalnofication的信息。你必须取消它并重新安排它。因此,在重新安排时,您必须使用现有的预定通知数组并重新安排(安排新通知并取消现有通知。这类似于您通过取消和重新安排来替换通知)

NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledNotifications];
i = 0;
for (UILocalNotification *notif in scheduledNotifications) {
  UILocalNotification *notifToReschedule;

  i++; 

  notifToReschedule.userInfo = [NSString stringWithFormat:@"%@",i];
  notifToReschedule.timeZone = notif.timeZone
  // you can use the already scheduled local notification's information to schedule another one. and cancel the one yo are iterating through. 

  [[UIApplication sharedApplication] scheduleLocalNotification:notifToReschedule];

  [[UIApplication sharedApplication] cancelLocalNotification:notif];
}