iPhoneアプリで、ローカル通知を実装したいと思っています。
NSUserDefaultsで数字を保存、その数字が1なら通知しない、2なら指定した時間に合わせて通知をするという内容です。
下記のコードで実装していますが、保存した値が1の場合でも通知が実装されてしまいます。
どうしたら良いでしょうか?
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//NSUserDefaults
NSUserDefaults *firstlaunch_D = [NSUserDefaults standardUserDefaults];alerm_D = [NSUserDefaults standardUserDefaults];
alerm = [alerm_D integerForKey:@"alerm"];//バッチを0にする
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;//このアプリ名義で登録しているローカル通知を削除
[[UIApplication sharedApplication] cancelAllLocalNotifications];// Override point for customization after application launch.
return YES;
}
-
(void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.if(alerm == 2){
[self localNotificationStart];
}if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil];
[application registerUserNotificationSettings:settings];
}
}
- (void)localNotificationStart
{
/*--ローカル通知を削除する--*/ //バッチを0にする [UIApplication sharedApplication].applicationIconBadgeNumber = 0; //このアプリ名義で登録しているローカル通知を削除する [[UIApplication sharedApplication] cancelAllLocalNotifications]; alerm_D = [NSUserDefaults standardUserDefaults]; alerm = [alerm_D integerForKey:@"alerm"]; time_D_1 = [NSUserDefaults standardUserDefaults]; time_1 = [time_D_1 integerForKey:@"time1"]; time_D_2 = [NSUserDefaults standardUserDefaults]; time_2 = [time_D_2 integerForKey:@"time2"]; /*--設定値を宣言する--*/ int targetDay = 0;//何日後に出すか NSInteger targetHour = time_1;//何時に出すか NSInteger targetMinute = time_2; /*--ローカル通知を削除する--*/ //バッチを0にするよ //[UIApplication sharedApplication].applicationIconBadgeNumber = -1; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:-1]; //このアプリ名義で登録しているローカル通知を削除する [[UIApplication sharedApplication] cancelAllLocalNotifications]; /*--日時操作に必要なものを用意--*/ //現在日時を取得するよ(標準日時だよ) NSDate *today = [NSDate date]; //日付操作に必要なカレンダーを作成する。 NSCalendar *cal = [NSCalendar currentCalendar]; //日付を取り出しやすくするコンポーネントを作成する。 NSDateComponents *comps = [[NSDateComponents alloc] init]; /*--指定の日付、時間に通知するロジック--*/ //タイムゾーンをiPhoneで指定したものに変更する [comps setTimeZone:[NSTimeZone systemTimeZone]]; /*現在日時をカレンダーを通して、年|月|日に分解して **コンポーネントに登録する **時分秒はいらないので登録しない **登録しなかったら0で登録される */ comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:today]; //コンポーネントに入れた日付に◯日後を追加する targetDay += comps.day; //日時をコンポーネントにセットし直す [comps setDay:targetDay]; [comps setHour:targetHour]; [comps setMinute:targetMinute]; //コンポーネントにセットした日時を標準日時に訳しなおす NSDate *date = [cal dateFromComponents:comps]; NSLog(@"date:%@",date); /*--通知を作ってセットする--*/ //通知を作るよ UILocalNotification *notification = [[UILocalNotification alloc]init]; //通知に時間関係をいろいろセットする notification.fireDate = date; notification.repeatInterval = NSWeekCalendarUnit; notification.timeZone = [NSTimeZone localTimeZone]; //通知で登録するバッチ数を決める notification.applicationIconBadgeNumber = 1; //通知で表示する文言を決める notification.alertBody = [NSString stringWithFormat:@"通知しました"]; //通知でユーザーに鳴らす音を決める notification.soundName = UILocalNotificationDefaultSoundName; notification.alertAction = @"鳴らしたい音"; //通知でユーザーにお知らせする文言を決める NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"通知したい文言" forKey:@"EventKey"]; //ユーザへのお知らせを登録する notification.userInfo = infoDict; //通知を登録する [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。