如何同时触发多个通知?

时间:2013-02-18 16:05:17

标签: iphone ios objective-c xcode uilocalnotification

我们可以同时为3个项目激发多个uilocalnotification,例如:当应用程序在后台/前台时,通知设置为在下午5:00触发..我应该收到3个通知应该在另一个。它只是触发最后设置的通知。我添加了代码。

    - (UILocalNotification *)scheduleNotification :(int)remedyID
    {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
        Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil)
        {
            NSString *descriptionBody;
            NSInteger frequency;
            UILocalNotification *notif = [[cls alloc] init];

            notif.timeZone = [NSTimeZone defaultTimeZone];
             for (int i=0; i<remedyArray.count; i++)
            {
                int arrayid = [[[remedyArray objectAtIndex:i] objectForKey:@"RemedyID"] intValue];
                if (arrayid == remedyID)
                {
                    descriptionBody=[[remedyArray objectAtIndex:i] objectForKey:@"RemedyTxtDic"];
                    frequency=[[[remedyArray objectAtIndex:i] objectForKey:@"RemedyFrequency"] integerValue];
                   break;
                }
            }
            NSArray *notificationFireDates = [self fireDatesForFrequency:frequency];
            for (NSDate *fireDate in notificationFireDates)      
  {
                Class cls = NSClassFromString(@"UILocalNotification");
                if (cls != nil)
                {

                    UILocalNotification *notif = [[cls alloc] init];                
                    notif.timeZone = [NSTimeZone defaultTimeZone];                
                    notif.repeatInterval = NSDayCalendarUnit;
                    notif.alertBody = [NSString stringWithString:descriptionBody];

                    notif.alertAction = @"Show me";
                    notif.soundName = UILocalNotificationDefaultSoundName;
                    notif.applicationIconBadgeNumber = 1;
                    notif.fireDate = fireDate;                

                    NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
    forKey:@"kRemindMeNotificationDataKey"];

                    notif.userInfo = userDict;                
                    [[UIApplication sharedApplication] scheduleLocalNotification:notif];

                }
            }     

            return notif;
        }
        else
        {
            return nil;
        }

    }

1 个答案:

答案 0 :(得分:0)

如果你说要像这样调用你的代码:

   [something scheduleNotification:remedyOne];
   [something scheduleNotification:remedyTwo];
   [something scheduleNotification:remedyThree];

然后,重写您的代码。因为你的代码有

[[UIApplication sharedApplication] cancelAllLocalNotifications];

scheduleNotification:方法的第一行。

所以这就是问题所在。