0

通知をスケジュールし、警告メッセージを表示する前に 60 分警告を与えます...

通知を追加するとすぐに、App Delegate のメソッドが呼び出されます。

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

バックグラウンドでアラートとして表示されることをどのように知ることができますか? 60分間隔でスケジュールされたアラートを確実に指定するために、オーバーライドまたは使用する必要がある他のデリゲートメソッドはありますか...

- (void)scheduleNotificationWithItem:(NSDate *)item interval:(int)minutesBefore
{   
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    //NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *currentDateComponents = [calendar components:( NSWeekdayCalendarUnit | 
                                                                     NSYearCalendarUnit | NSMonthCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSWeekCalendarUnit | NSMinuteCalendarUnit) fromDate:item];

    NSLog(@"- current components year = %i , month = %i , week = % i, weekday = %i", [currentDateComponents year], [currentDateComponents month], [currentDateComponents week], [currentDateComponents weekday]);

    NSLog(@"[currentDateComponents minute]: %i",    [currentDateComponents minute]);
    NSLog(@"[currentDateComponents hour]: %i",      [currentDateComponents hour]);
    NSLog(@"[currentDateComponents day]: %i",       [currentDateComponents day]);
    NSLog(@"[currentDateComponents week]: %i",      [currentDateComponents week]);
    NSLog(@"[currentDateComponents month]: %i",     [currentDateComponents month]);
    NSLog(@"[currentDateComponents year]: %i",      [currentDateComponents year]);

    [dateComps setDay: [currentDateComponents day]];

    [dateComps setMonth:[currentDateComponents month]];

    [dateComps setYear:[currentDateComponents year]];

    [dateComps setHour:[currentDateComponents hour]];

    [dateComps setMinute:[currentDateComponents minute]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    if (localNotif == nil)
        return;

    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];

    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = [NSString stringWithFormat:@"%@\n%@",
                            streetAddress,
                            stringOfWhenAuctionIsOn];

    localNotif.alertAction = NSLocalizedString(@"View Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;

    localNotif.applicationIconBadgeNumber = 1;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:streetAddress
                                                         forKey:idOfStreetAlert];

    localNotif.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
4

2 に答える 2

1

通知がすぐに表示される場合は、fireDate プロパティに問題がある可能性があります。送信された日付が正しいことを確認しようとしているようですが、fireDate が期待どおりであることも確認する必要があります。

また、おそらく行うことができます

localNotif.fireDate = [item dateByAddingTimeInterval:-60*minutesBefore];

同じ効果を達成し、いじる必要はありませんNSDateComponents。私はこれをテストしていませんが、うまくいくかもしれません。

タイマーが起動し、アプリが実行されていない場合に何が起こるかについて。アプリが終了していない場合は、application:didReceiveLocalNotification:呼び出されます。反対側でアプリが終了した場合はapplication:didFinishLaunchingWithOptions:、@Ishu の指摘に従ってチェックインする必要があります。コードの重複を避けるために、通常は通知を共通のメソッドに渡すだけです。

于 2011-02-16T08:10:07.740 に答える
0

バックグラウンドのデリゲートメソッドはありません。didFinishLaunchingWithOptionsでコードを記述する必要があります。

UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (localNotif) {
        //your code
        NSLog(@"Recieved Notification %@",localNotif);
    }

アプリがバックグラウンドから通知を受け取るときにロジックを作成します。通知を設定したアプリが通知を受け取ることを心配しないでください。

于 2011-02-16T04:14:11.347 に答える