1

私のアプリケーションでは、アプリケーションがバックグラウンドで強制終了された場合、特定の時間にローカル通知を設定したいのですが、アプリケーションが実行中の場合は、ローカル通知を表示したくありません

ローカル通知用のコードを書きました

NSCalendar *calendar1 = [NSCalendar autoupdatingCurrentCalendar];

        // Get the current date
        NSDate *pickerDate1 =self.dateEvening;//[self.dateEvening dateByAddingTimeInterval:60*1];// [self.pickerView date];

        // Break the date up into components
        NSDateComponents *dateComponents1 = [calendar1 components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) 
                                                       fromDate:pickerDate1];
        NSDateComponents *timeComponents1 = [calendar1 components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) 
                                                       fromDate:pickerDate1];


        NSDateComponents *dateComps1 = [[NSDateComponents alloc] init];
        [dateComps1 setDay:[dateComponents1 day]];
        [dateComps1 setMonth:[dateComponents1 month]];
        [dateComps1 setYear:[dateComponents1 year]];
        [dateComps1 setHour:[timeComponents1 hour]];

        [dateComps1 setMinute:[timeComponents1 minute]];
        [dateComps1 setSecond:[timeComponents1 second]];
        NSDate *itemDate1 = [calendar1 dateFromComponents:dateComps1];
        [dateComps1 release];

        UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
        if (localNotif1 == nil)
            return;
        localNotif1.fireDate = itemDate1;
        localNotif1.timeZone = [NSTimeZone defaultTimeZone];
        localNotif1.alertBody = @"Open application";


        localNotif1.alertAction = @"View";

        localNotif1.soundName = UILocalNotificationDefaultSoundName;




        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
        [localNotif1 release];

上記のコードは、アプリケーションがバックグラウンドから閉じられる場合は正常に機能しますが、アプリケーションが実行中の場合は、ローカル通知を発生させたくありません。

4

0 に答える 0