4

local notifications私のアプリケーション で有効にするのはかなり困難でした。

そして、ええ、アプリが1週間local notification閉じられた後にスケジュールしたい魔女とプロジェクトをセットアップしました. たとえば、ユーザーが土曜日の 8 日にアプリを開いた場合、ローカル通知は次の土曜日の 15 日に表示されますが、時刻は変更されます。たとえば、彼/彼女は午後 8 時にアプリを閉じますが、来週の午後 8 時に邪魔したくないので、すべての通知を午後 6 時に表示したいなどです。

これで私の問題がわかりました。これが私が使用しているコードです。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];

    //set day (saturday)

    [componentsForReferenceDate setDay:26] ;
    [componentsForReferenceDate setMonth:1] ;
    [componentsForReferenceDate setYear:2013] ;

    [componentsForReferenceDate setHour: 17] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];

    // Create the notification

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


    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Du wirst vermisst! \nDeine App braucht dich, schreibe sie zu Ende!"] ;
    notification.alertAction = @"Zurückkehren";
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName = @"Appnotifisound.wav";
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

}

バッジと を削除する方法が他にもある必要があることはわかっていますが、これには重要ではないdidrecievenotificationため、それらを除外しました。

このコードを使用して、毎週土曜日の午後 5 時 30 分(ドイツ) に通知をスケジュールすることができました。しかし、アプリがちょうど1週間閉じられたときに、一度だけスケジュールしたかったのです。それはどういうわけか可能ですか?誰かが私のコードを修正するか、これに対する解決策を教えていただければ幸いです。

よろしくお願いします。この長い投稿を読んでいただきありがとうございます。

ノア

4

3 に答える 3

6

以前の通知のスケジュールを解除する必要があります。

for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
    {
        if ([[lNotification.userInfo valueForKey:@"information"] isEqualToString:@"Some information"])
        {
            [[UIApplication sharedApplication]cancelLocalNotification:lNotification];
            break;
        }
    }

以下の方法で発火日を設定します。

NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                     fromDate:[NSDate date]];
    [componentsForReferenceDate setHour: 17] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *tempDate = [calendar dateFromComponents: componentsForReferenceDate];
    [componentsForReferenceDate release];

    NSDateComponents *comps = [[NSDateComponents alloc]init];
    [comps setDay:7];
    NSDate *fireDateOfNotification = [calendar dateByAddingComponents:comps
                                                               toDate:tempDate options:0]
    [comps release];
于 2013-03-06T16:17:24.630 に答える
1

これnotification.repeatInterval = NSWeekCalendarUnitを引き起こしています。使用する:

notification.repeatInterval = 0;

これにより、繰り返しが防止されます(ソース)。

于 2013-03-06T16:15:02.540 に答える
1

これがSwift 2.0の適応です

現在の通知のスケジュール解除

    for notifMe:AnyObject in UIApplication.sharedApplication().scheduledLocalNotifications!{
        let title = notifMe.userInfo["information"] as? String
        if title == "some information"{
                UIApplication.sharedApplication().cancelLocalNotification(notifMe as! UILocalNotification)
        }
        }

通知日の設定

    let date = NSDate()
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components(NSCalendarUnit.Year.union(NSCalendarUnit.Month).union(NSCalendarUnit.Day),fromDate: date)
    components.hour = 17
    components.minute = 30
    components.second = 00

    let tempDate = calendar.dateFromComponents(components)!
    let comps = NSDateComponents()
    comps.day = 7
    let fireDateOfNotification = calendar.dateByAddingComponents(comps, toDate: tempDate, options:[])
于 2015-11-03T14:55:41.133 に答える