2016-08-15 16 views
-1

おはよう、 localNotificationに2週間(14days)ごとに通知する方法。2週間ごとにUILocalNotificationに通知する方法

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

    timer = [NSTimer scheduledTimerWithTimeInterval:60*60*24*14 target:self selector:@selector(getNotifiedForTwoWeeks:) userInfo:nil repeats:YES]; 


} 

-(void)getNotifiedForTwoWeeks:(id)userinfo{ 

     // Schedule the notification 
    UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
    localNotification.fireDate = [NSDate date]; 
    localNotification.alertBody = @"Notification Message"; 
    localNotification.alertAction = @"Show me the item"; 
    localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
} 

この実装は正しいかどうかを教えてください。 2週間ごとにLocalNotificationメッセージを通知するための最善の方法がありますか?

貴重なご意見をいただきありがとうございます。

+0

fireDaterepeatIntervalプロパティを宣言し、14日ごとのスケジュールすることができます2週間。 'fireDate'を将来の日付に更新する必要があります(また、60 * 60 * 24 * 12ではなく、NSCalendar'の計算を行う必要があります)。 http://stackoverflow.com/a/27424355/1271826を参照してください。 – Rob

+0

2週間ごとではなく、毎週または毎月であれば、 'repeatInterval'を使うことができますが、2週間ごとに、その答えに示されているように自分でスケジュールを設定する必要があると思います。 – Rob

+0

@NSTimerは、指定された通知宣言メソッドの特定のメソッドをトリガするためにTimeIntervalをスケジュールするために使用されます。 – kiran

答えて

0

アプリがバックグラウンドで入力されると、しばらくするとシステムが停止し、NSTimerはスケジュールされません。 だから、通知が通じことが唯一のアプリがアクティブである場合に動作するようになるだろうと私は、これは `NSTimer`が何のためにあるのかわからないんだけどUILocalNotification

UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.fireDate = [NSDate dateByAddingTimeInterval:60*60*24*14]; 
localNotification.alertBody = @"Notification Message"; 
localNotification.alertAction = @"Show me the item"; 
localNotification.timeZone = [NSTimeZone timeZoneWithName:@"GMT"]; 
localNotification.repeatInterval = NSDayCalendarUnit; 

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
+0

言うまでもなく、 'beginBackgroundTaskWithName'は、60 * 60 * 24 * 14のためにスケジュールされたタイマーに関しては無用です。とにかく、このユースケースでは、タイマー全体がデッドエンドです。数週間ではなく、数分でいいです。 – Rob

+0

あなたは正しいです – iSashok

関連する問題