0

我是 iPhone 应用程序开发的新手。我正在做警报应用程序。在这个应用程序中,我使用本地通知。我在完成按钮操作中调用通知方法。现在我点击完成按钮意味着通知正确触发。然后再次点击意味着也被解雇了,一次又一次的按下意味着工作错误,但我使用的是通知触发日期时间。此时完成意味着用户再次单击完成按钮意味着再次触发通知。

我想在那个特定的时间开火。你能帮我么。

我在这里使用这个源代码。

-(IBAction)doneButton
{

    [self scheduledNotification];

}

-(void)scheduledNotification
{
    Resource *resourceLoader = [[Resource alloc] init];

     NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:@"preference"];

    if ([@"ON" isEqualToString:[prefDic objectForKey:@"alarm"]]) 
    {

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

        Class cls = NSClassFromString(@"UILocalNotification");
        if (cls != nil) 
        {
            NSString *musicName;
            //NSDate *selectedDateTime = [[NSDate alloc]init];

            selectedDateTime = [prefDic objectForKey:@"alarmtime"];
            NSLog(@"saravanan periyasamy %@", selectedDateTime);

            musicName = [NSString stringWithFormat:@"%@%@",[prefDic objectForKey:@"alarmmusic"],@".wav" ];

            NSLog(@"musicname %@", musicName);

            NSLog(@"saravanan periyasamy123 %@", selectedDateTime);
            UILocalNotification *notif = [[cls alloc] init];

            notif.fireDate = selectedDateTime;  /* time for fireDate*/

            NSLog(@"selectedtime %@",selectedDateTime);

            notif.timeZone = [NSTimeZone systemTimeZone];

            notif.alertBody = @"Alarm";
            notif.alertAction = @"Show me";
            notif.repeatInterval = 0;

            notif.soundName = musicName;
            notif.applicationIconBadgeNumber = 1;
            NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"saravanan"
                                                                 forKey:kRemindMeNotificationDataKey];
            notif.userInfo = userDict;
            [[UIApplication sharedApplication] scheduleLocalNotification:notif];
            [notif release];
        }
    }
}


AppDelegate.m



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   Class cls = NSClassFromString(@"UILocalNotification");

    if (cls) {
        UILocalNotification *notification = [launchOptions objectForKey:
                                             UIApplicationLaunchOptionsLocalNotificationKey];

        if (notification) {
            NSString *reminderText = [notification.userInfo 
                                      objectForKey:kRemindMeNotificationDataKey];
            //[self.viewController showReminder:reminderText];
            [self.settingViewController showReminder1:reminderText];
        }
    }
    application.applicationIconBadgeNumber = 0;   
    return YES;    

}

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

    application.applicationIconBadgeNumber = 0;
}

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {



    application.applicationIconBadgeNumber = 0;
    NSString *reminderText = [notification.userInfo
                              objectForKey:kRemindMeNotificationDataKey];
    //[self.viewController showReminder:reminderText];
    [self.settingViewController showReminder1:reminderText];
}
4

2 回答 2

0

嗨,我不明白你的问题,但在这里我发布我的 LocalNotification 示例代码只是比较它或使用这个例子....

在这里当用户点击完成按钮然后调用“btnGo_Clicked”方法

-(IBAction)btnGo_Clicked:(id)sender{

    NSLog(@"\n ----------->>Go Clicked");
    Class cls = NSClassFromString(@"UILocalNotification");
    if (cls != nil) {
        NSString *kRemindMeNotificationDataKey = @"kRemindMeNotificationDataKey";


        NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSQuarterCalendarUnit fromDate:[exdatePicker date]];
        [dc setDay:dc.day - 1];
        NSDate *noticeDate = [[NSCalendar currentCalendar] dateFromComponents:dc];


        NSDateFormatter* dateFormatterstring = [[NSDateFormatter alloc] init];
        [dateFormatterstring setDateFormat:@"dd-MM-yyyy hh:mm:ss a"];
        NSString *dateString = [dateFormatterstring stringFromDate:noticeDate];

        txtExpirayDate.text = dateString;
        UILocalNotification *notification = [[cls alloc] init];

        notification.fireDate = noticeDate;
        notification.timeZone = [NSTimeZone defaultTimeZone];
        notification.alertBody = [NSString stringWithFormat:@"%@ your Licence Expiry Date is Tommorow",txtLicence.text];
        notification.alertAction = @"Show me";
        notification.soundName = UILocalNotificationDefaultSoundName;
        notification.applicationIconBadgeNumber = 1;
        NSDictionary *userDict = [NSDictionary dictionaryWithObject:txtLicence.text forKey:kRemindMeNotificationDataKey];
        notification.userInfo = userDict;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
        [notification release];
    }
    exdatePicker.hidden=YES;
}

做一些改变,你得到你的输出

我希望这个答案对你有帮助......

:)

于 2012-08-21T09:51:35.250 回答
0

如果您想多次按下按钮,请删除此行。

[[UIApplication sharedApplication] cancelAllLocalNotifications];  

因为它删除了您以前的所有通知。

于 2012-08-21T09:51:40.540 回答