本地通知问题?

时间:2012-08-21 09:45:53

标签: iphone localnotification

我是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];
}

2 个答案:

答案 0 :(得分: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;
}

进行一些更改并获得输出

我希望这个答案可以帮助你...

:)

答案 1 :(得分:0)

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

[[UIApplication sharedApplication] cancelAllLocalNotifications];  

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