【问题标题】:How to fire multiple notification at the same time?如何同时触发多个通知?
【发布时间】:2013-02-03 03:01:34
【问题描述】:

我们可以同时为 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;
        }

    }

【问题讨论】:

  • “一个接一个地同时”听起来很矛盾……
  • 我的意思是,如果要触发 3 个通知,它应该像在队列中一样一个接一个地出现在通知栏中

标签: iphone ios objective-c xcode uilocalnotification


【解决方案1】:

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

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

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

[[UIApplication sharedApplication] cancelAllLocalNotifications];

scheduleNotification: 方法的第一行。

这就是问题所在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2012-01-06
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多