多个本地通知问题iOS

时间:2014-10-15 07:37:38

标签: objective-c ios7 uilocalnotification

我想根据来自服务器的通知计数安排多个通知,它应该一个接一个地点火...... 如果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 个答案:

答案 0 :(得分:0)

我创建了一个只通过传递时间和消息来创建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]每条消息。

相关问题