【问题标题】:Multiple local notification issue iOS多个本地通知问题 iOS
【发布时间】:2014-10-15 07:37:37
【问题描述】:

我想根据来自服务器的通知计数安排多个通知,它应该一个接一个地触发... 如果count == 4,应该触发四个通知..

我是 iOS 初学者

这是我的代码:

- (void)sendNotification {
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    localNotification.alertBody = @"We have fresh vegetables and fruits!";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)test {
    NSData *allNoteData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"hiberryfarm.com/mobile/all_notification.php"]];
    NSError *error;
    NSMutableDictionary *allNote = [NSJSONSerialization JSONObjectWithData:allNoteData options:NSJSONReadingMutableContainers error:&error];
    NSArray *Notifications = allNote[@"notifications"];
    NSUInteger *noteCount = [Notifications count];
    if (noteCount > 0) {
        for ( NSDictionary *noteObj in Notifications ) {
            [self sendNotification];
        }
}

【问题讨论】:

  • 几件事 1) 你已经尝试过什么?请分享您已经完成的代码,以向我们展示您在提出问题之前自己尝试过此操作,我们很乐意提供帮助,但您需要表明您首先尝试自己解决此问题。 2) 这与xcode IDE 无关,因为您使用xcode 并不意味着您应该使用该标签,所以我删除了该标签。欢迎使用 stackoverflow。
  • 同意@Popeye!您需要更加准确,并就您已经做过的事情向我们提供提示,否则将无法真正提供帮助。
  • 感谢您的快速回复。不要介意他们在那里犯错。下面是我尝试的代码 appviewcontroller.m
  • - (void)sendNotification { UILocalNotification *localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; localNotification.alertBody = @"我们有新鲜蔬菜和水果!"; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; }
  • 请将代码添加到问题中。谢谢

标签: objective-c ios7 uilocalnotification


【解决方案1】:

我创建了一个方法,它通过传递时间和消息来创建UILocalNotifications

+ (void)createNotificationWithNumberOfDays:(int)days 
                                andMessage:(NSString *)message 
                                  userInfo:(NSDictionary *)dict{

    NSCalendar *gregorian = 
    [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:days];
//    [components setSecond:days * 2];
    NSDate *dateAlert = 
     [gregorian dateByAddingComponents:components 
                                toDate:[NSDate date] options:0];

    UIApplication *app = [UIApplication sharedApplication];
    UILocalNotification *notifyAlarm = [[UILocalNotification alloc] init];

    if( notifyAlarm ){
        [notifyAlarm setFireDate:dateAlert];
        [notifyAlarm setTimeZone:[NSTimeZone defaultTimeZone]];
        [notifyAlarm setSoundName:@"soundname"];
        [notifyAlarm setAlertBody:message];
        [notifyAlarm setUserInfo:dict];
        [app scheduleLocalNotification:notifyAlarm];
    }
}

听起来应该只是单个UILocalNotifications 之间的第二个区别。因此,您必须在每条消息中再添加[components setSecond:@1]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多