ローカル通知が必要なiPhoneアプリを作成しています。
ローカル通知にはrepeatIntervalプロパティがあり、ミンチュート、時間、日、週、年などの単位繰り返し間隔を設定できます。
繰り返し間隔は4時間にします。
したがって、4時間ごとにローカル通知が届きます。
ユーザーがそれぞれに個別の通知を設定することは望ましくありません。
ユーザーがrepeatIntervalを4時間に設定できるようにしたいと思います。
それ、どうやったら出来るの?
ローカル通知が必要なiPhoneアプリを作成しています。
ローカル通知にはrepeatIntervalプロパティがあり、ミンチュート、時間、日、週、年などの単位繰り返し間隔を設定できます。
繰り返し間隔は4時間にします。
したがって、4時間ごとにローカル通知が届きます。
ユーザーがそれぞれに個別の通知を設定することは望ましくありません。
ユーザーがrepeatIntervalを4時間に設定できるようにしたいと思います。
それ、どうやったら出来るの?
答えを得た、それはそれが得るのと同じくらいまっすぐです。
カスタムの繰り返し間隔を作成することはできません。
NSCalendarUnit の組み込み Unit Time Intervals で使用する必要があります。
上記の解決策をすべて試し、他のものも試しましたが、どちらも機能しませんでした。
カスタムの時間間隔で機能させる方法がないことを発見するのに十分な時間を費やしました。
繰り返し間隔はビットマップ定数を持つ単なる列挙repeatIntervals
型であるため、毎分、毎秒、毎週など、カスタムを設定する方法はありません。
そうは言っても、ユーザーがそれぞれを設定しなければならない理由はありません。Frequency unit: Yearly/Monthly/Weekly/Hourly
ユーザーインターフェイスで「 」や「」などの2つの値を設定させるとEvery ____ years/months/weeks/hours
、適切な発火日を繰り返すことなく設定することで、適切な通知を自動的に生成できます。
UILocalNotification *locNot = [[UILocalNotification alloc] init];
NSDate *now = [NSDate date];
NSInterval interval;
switch( freqFlag ) { // Where freqFlag is NSHourCalendarUnit for example
case NSHourCalendarUnit:
interval = 60 * 60; // One hour in seconds
break;
case NSDayCalendarUnit:
interval = 24 * 60 * 60; // One day in seconds
break;
}
if( every == 1 ) {
locNot.fireDate = [NSDate dateWithTimeInterval: interval fromDate: now];
locNot.repeatInterval = freqFlag;
[[UIApplication sharedApplication] scheduleLocalNotification: locNot];
} else {
for( int i = 1; i <= repeatCountDesired; ++i ) {
locNot.fireDate = [NSDate dateWithTimeInterval: interval*i fromDate: now];
[[UIApplication sharedApplication] scheduleLocalNotification: locNot];
}
}
[locNot release];
これを行う方法が1つあります。これをプロジェクトに実装しました
まず、開始日 (たとえば、1 分ごと) でローカル通知を作成します。次のステップ - ユーザー情報に、この通知の一意の ID を入力し (後で削除する場合)、カスタム期間を次のように入力します。
-(void) createLocalRepeatedNotificationWithId: (NSString*) Id
{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSTimeInterval your_custom_fire_interval = 60; // interval in seconds
NSDate *remindDate = [[NSDate date] dateByAddingTimeInterval:your_custom_fire_interval];
localNotification.fireDate = remindDate;
localNotification.userInfo = @{@"uid":Id, @"period": [NSNumber numberWithInteger:your_custom_fire_interval]};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
その後、-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
AppDelegate に実装します。
保留中の通知に再度追加するだけです。
NSInteger period = [[notification.userInfo objectForKey:@"period"] integerValue]; //1
NSTimeInterval t= 10 * period;
notification.fireDate =[[NSDate date] dateByAddingTimeInterval:t]; //2
[[UIApplication sharedApplication] scheduleLocalNotification:notification]; //3
この通知を削除したい場合は、
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"id"]];
if ([uid isEqualToString:notification_id_to_remove])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
非常に重要です。
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
あなたがバックグラウンドにいるときは、呼び出さないでください。そのため、長時間実行されるバックグラウンド タスクをセットアップする必要があり、ここで通知を再度作成します。
私はこのコードを使用しましたが、繰り返し LocalNotification に対して正常に動作しています このコードの実装には LavaSlider コードを使用しました
UILocalNotification * localNotifEndCycle = [[UILocalNotification alloc] init];
localNotifEndCycle.alertBody = @"Your Expected Date ";
NSDate *now = [NSDate date];
for( int i = 1; i <= 10;i++)
{
localNotifEndCycle.alertBody = @"Your Expected Date ";
localNotifEndCycle.soundName=@"best_guitar_tone.mp3";
localNotifEndCycle.fireDate = [NSDate dateWithTimeInterval:180*i sinceDate:now];
[[UIApplication sharedApplication] scheduleLocalNotification: localNotifEndCycle];
}
}
通知を起動するたびに個別の通知を設定する場合は、任意の時間間隔を設定できます。次に、それらを管理する必要があります。現在、バックグラウンドでの実行が許可されているアプリでない場合は、ユーザーにアプリを実行させて更新する必要があります。これを達成したことで、これは主要な PITA であると断言できます。
notif.repeatInterval = NSDayCalendarUnit;