iPhoneアプリでローカル通知が実装できません
解決済
回答 1
投稿 ・編集
- 評価
- クリップ 0
- VIEW 1,783
iPhoneアプリで、ローカル通知を実装したいと思っています。
NSUserDefaultsで数字を保存、その数字が1なら通知しない、2なら指定した時間に合わせて通知をするという内容です。
下記のコードで実装していますが、保存した値が1の場合でも通知が実装されてしまいます。
どうしたら良いでしょうか?
//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;
}
// 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];
}
}
{
/*--ローカル通知を削除する--*/
//バッチを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];
}
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 {
// 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];
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
コードを見た限り、
に再現しそうな気がします。
alerm変数がdidFinishLaunchingWithOptionsで初期化された移行に更新されていないので、アプリ内でNSUserDefaultのalermキーを更新した場合はバックグラウンドに入る前に再度alerm変数初期化する必要があります。
起動時に2が登録されているとき
に再現しそうな気がします。
alerm変数がdidFinishLaunchingWithOptionsで初期化された移行に更新されていないので、アプリ内でNSUserDefaultのalermキーを更新した場合はバックグラウンドに入る前に再度alerm変数初期化する必要があります。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.20%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる