2017-08-14 3 views
0

私のViewControllerの1つにローカル通知を作成しています。iOSで1回だけ繰り返すLocalNotificationをスケジュールする方法

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

私のテスト目的のために、30秒後に予定された時間を設定しました。

localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:30]; 

私はそのローカル通知私は、通知をスケジュール結局のところ

localNotification.alertBody = @"Alert message"; 
    localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; 
    localNotification.soundName = UILocalNotificationDefaultSoundName; 

のためにいくつかのより多くのプロパティを追加しました。

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

私は30秒後に通知を予定していました。

私が必要とするのは、1分後にその通知をもう一度繰り返すことです。これはあらかじめ定義されたスヌーズ時間のようなものです。

私はrepeatIntervalプロパティをを追加する場合は、以下のように、それは一人一人分で起動します。

localNotification.repeatInterval = NSCalendarUnitMinute; 

ターゲットを達成する方法はありますか?要約として

は、私が必要なものをロックされているfireDate値と背景と電話で唯一の他の事前定義されたスヌーズ時間でもアプリケーションにローカル通知を発射です。

注:これについての別のローカル通知を作成することはお勧めしません。

+1

でスケジュール時間焼成+ 1分です。通知をタップすると、2番目の通知が手動で削除されます。それ以外の場合は、両方の通知が特定の発射日に1つずつ発生します。 –

+0

通知は繰り返されるか、通知されません。繰り返し数を指定する機能はありません – Paulw11

+0

@YagneshDobariya私は_Note_の部分に気付かなかったと思います。私はこれ以外のオプションを探しています。 – AnujAroshA

答えて

-2

最初にnotificationの1分後にメソッドを呼び出すと、notificationが1回だけ呼び出されます。あなたは通知でuserInfoを設定することができます

[NSTimer scheduledTimerWithTimeInterval:60.0 
    target:self 
    selector:@selector(targetMethod:) 
    userInfo:nil 
    repeats:NO]; 
1

NSMutableDictionary *payload = [NSMutableDictionary dictionary]; 
payload[@"repeat"] = [NSNumber numberWithBool:YES]; 
payload[@"interval"] = @(1.0); 

通知を設定した後、ユーザー情報を追加すること

.... 
localNotification.alertBody = @"Alert message"; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
.... 
localNotification.userInfo = payload; 

didReceiveLocalNotificationデリゲートメソッドで

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    NSDictionary *dict = [notification userInfo]; 
    BOOL repeat = [dict[@"repeat"] boolValue]; 
    if (repeat) { 
      //update firedate and schedule notification 
    } 
} 
+0

賢い解決策。 –

+0

@Akhilrajtr私はすでにその方法を試しました。電話がロックされている場合、これは機能しません。これは、didReceiveLocalNotificationメソッドを起動することを意味します。アプリケーションはフォアグラウンドで行うか、ユーザーが最初の通知アラートをタップする必要があります。 – AnujAroshA

+0

@AnujAroshA ohk。その場合、2つの通知を追加する以外の方法は考えられません。ここに誰かが助けることができるかもしれません。がんばろう。 – Akhilrajtr

0

セット​​

0

@ Akhilrajtrのソリューションはほぼ正しいです。

通知のユーザー情報を更新し、didReceiveLocalNotificationで処理することができます。また、これに加えて、登録通知設定が必要です。

AppDelegate。メートル

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationActivationModeBackground | UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil]; 
    [application registerUserNotificationSettings:settings]; 
    return YES; 
} 

NB:UIUserNotificationSettingsUNNotificationSettings代わりにiOSの10.に廃止されました。

ソリューション:

1.AddのUserInfo通知オブジェクトへ

localNotification.userInfo = @{@"repeat" : @(YES), @"interval" : @(1.f)}; 

2.Updateあなたは火の日と別で2通知をスケジュールする必要がdidReceiveLocalNotification

関連する問題