iPhone中的多个本地通知 [英] Multiple Local Notification in iPhone

查看:143
本文介绍了iPhone中的多个本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有朋友列表说:3个朋友,这三个都有生日详情。我需要安排本地通知,以显示他们的b'days警报。我知道并处理了本地通知,但我将如何处理这些多重通知?

In my app, I have list of friends say: 3 friends, all these three have birthday details. I need to schedule local notification to show their b’days alert. I know and handled a local notification but how will I handle these multiple notification?

我在for loop中设置了开火日期。这是正确的,请参阅代码。

I am setting fire date in "for loop".Is it proper, See the code.

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
        return;
    NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
    for (int i = 0; i< [delegate.viewController.contactList count] ; i++) {
        NSString *birthday = [[myArray objectAtIndex:i]objectForKey:@"birthday"];

        [formatter setDateFormat:@"MM/dd/yyyy"];
        NSDate *date = [formatter dateFromString:birthday];

        localNotif.fireDate = [date dateByAddingTimeInterval:10];
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        NSLog(@"local %@",localNotif.fireDate);
    }

    localNotif.applicationIconBadgeNumber = 1;

    NSString *itemName = @"Friend Name";
    NSDictionary *userDict = [NSDictionary dictionaryWithObjectAndKey:itemName,@"msg", nil];

    localNotif.userInfo = userDict;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

我想要什么,在这种方法中我只需要根据他们的日期为所有朋友设置通知。任何人都可以告诉我我在哪里做错了,如果我遗失了什么,请通知我。

What I want,in this method only I have to set notification for all the friend based on their date. can any one tell me where I’m doing wrong and if I’m missing anything please inform me.

推荐答案

只需要三个(或更多)本地通知并使用 scheduleLocalNotification:安排每个通知,问题是什么?
例如,这就是我在项目中所做的:

Just make three (or more) local notifications and schedule every one of them with scheduleLocalNotification:, what's the problem? For example this is what I did in my project:

for (int i = 0; i < 6; i++) {
    UILocalNotification *localNotification = [prototypeNotification copy];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:dates];
    [notifications addObject:localNotification];


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

    [localNotification release];
}

UPD

// ...this goes earlier:
static NotificationController *sharedNotificationController = nil;

- (id) init 
{
    if (self = [super init]) {
        notifications = [[NSMutableArray alloc] init];

        prototypeNotification = [[UILocalNotification alloc] init];
        prototypeNotification.repeatCalendar = [NSCalendar currentCalendar];
        prototypeNotification.repeatInterval = NSMinuteCalendarUnit;

        prototypeNotification.timeZone = [NSTimeZone defaultTimeZone];
        prototypeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
        prototypeNotification.applicationIconBadgeNumber = 0;
        prototypeNotification.alertBody = NSLocalizedString(@"Body", nil);
        prototypeNotification.alertAction = NSLocalizedString(@"Action", nil);

        enabled_ = NO;
    }
    return self;
}

这篇关于iPhone中的多个本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆