UILocalNotification或推送通知

时间:2013-03-16 19:48:53

标签: iphone ios push-notification uilocalnotification

在我的应用程序中,我有一系列朋友的生日,我需要在有人的生日来临时通知用户。数组将有大约200个朋友的生日。在这种情况下,

UILocalNotification会不会有效,正如Apple所说Each application on a device is limited to the soonest-firing 64 scheduled local notifications.如果是,我必须如何实施UILocalNotication。

我不想参加PushNotification.任何建议都会受到赞赏。

我正在安排这样的本地通知: -

-(void) scheduleNotification{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [self.notifications removeAllObjects];
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        NSString *name = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:NAME_KEY];
        NSString *birthday = [[delegate.viewController.contactList objectAtIndex:i]objectForKey:BIRTHDAY_KEY];
        if (birthday) {
            [formatter setDateFormat:@"MM/dd/yyyy"];
            [formatter setLocale:[NSLocale currentLocale]];
            [formatter setTimeZone:[NSTimeZone systemTimeZone]];
            NSDate *date = [formatter dateFromString:birthday];
            if (date == nil) {
                [formatter setDateFormat:@"MM/dd"];
                [formatter setLocale:[NSLocale currentLocale]];
                [formatter setTimeZone:[NSTimeZone systemTimeZone]];
                date = [formatter dateFromString:birthday];
            }
            NSCalendar *gregorianEnd = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *componentsEnd = [gregorianEnd components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];

            componentsEnd.year = [[NSDate date] year];
            date = [gregorianEnd dateFromComponents:componentsEnd];
            self.alarmTime = [date dateByAddingTimeInterval:self.mTimeInterval];
            localNotification.fireDate = _alarmTime;
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.applicationIconBadgeNumber = 1;

            NSString *itemName = @"B'day Alert!!!";
            NSString *msgName = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];
            NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:itemName,MessageKey, msgName,TitleKey, nil];
            localNotification.userInfo = userDict;
            localNotification.soundName = self.soundName;
            localNotification.alertBody = [NSString stringWithFormat:@"Celebrate %@'s B'day",name];


            [self.notifications addObject:localNotification];
            } 
        }
    }
}

我已将applicationDidEnterBackground委托实现为:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    UILocalNotification* notification;

    for (int i = 0; i< [self.settingVC.notifications count]; i++) {

        notification = [self.settingVC.notifications objectAtIndex:i];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }

}

另外,在didReceiveLocalNotification委托中我有这个:

 - (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSString *itemName = [notification.userInfo objectForKey:TitleKey];
    NSString *messageTitle = [notification.userInfo objectForKey:MessageKey];
    [self _showAlert:itemName withTitle:messageTitle];
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}

我应该在上述功能中做出哪些改变。

1 个答案:

答案 0 :(得分:2)

您仍然可以使用UILocalNotification,并且只安排64个最接近的生日。在应用程序中有活动的任何时候重新安排这些,以便更新最新的活动。我想象64次通知会让用户在需要再次启动应用程序之前有足够的时间。