質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Q&A

0回答

3107閲覧

iOS10でローカル通知を指定日時からリピート通知させたい

fumiasi

総合スコア12

Objective-C

Objective-Cはオブジェクト指向型のプログラミング言語のひとつです。C言語をベースにSmalltalkが取り入れられています。

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

0グッド

1クリップ

投稿2017/01/29 06:13

編集2017/02/12 06:42

UILocalNotificationから
iOS10以降のUNMutableNotificationContentにバージョンアップ作業をしているのですが、
指定の日付から毎日繰り返すにはどうすれば良いのでしょうか。

###コード

Objective

1// 初回再生時間 2日後の午前8時30分 2int day = 2; 3int hour = 8; 4int minute = 30; 5 6NSDate *nowDate = [NSDate date]; 7NSDateComponents *NowTimeComps = [Calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:nowDate]; 8 9int nowTime = (((int)NowTimeComps.hour * 60 ) + (int)NowTimeComps.minute) * 60 + (int)NowTimeComps.second; 10int setTime = day * 24 * 60 * 60; 11setTime += hour * 60 * 60; 12setTime += minute * 60; 13 14int setTimeDiff = setTime - nowTime; 15 16NSDate * alertDate = [[[NSDate alloc] initWithTimeInterval:setTimeDiff sinceDate:nowDate] autorelease];

Objective

1// UILocalNotification(iOS -9) 2// この処理の内容をバージョンアップさせる予定です。 3UILocalNotification *notification = [[UILocalNotification alloc] init]; 4notification.fireDate = alertDate; // <-通知させる日時を指定する 5notification.repeatInterval = NSCalendarUnitDay; // <-日付単位でリピートする 6notification.timeZone = [NSTimeZone localTimeZone]; 7notification.alertTitle = "タイトル"; 8notification.alertBody = "内容"; 9notification.soundName = UILocalNotificationDefaultSoundName; 10notification.alertAction = "OK"; 11 12[[UIApplication sharedApplication] scheduleLocalNotification:notification];

Objective

1// UNMutableNotificationContent(iOS 10-) 2// バージョンアップ用に処理を記述しましたが、指定日からリピート通知出来ません。 3UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init]; 4content.title = [NSString localizedUserNotificationStringForKey:"タイトル" arguments:nil]; 5content.body = [NSString localizedUserNotificationStringForKey:"内容" arguments:nil]; 6content.sound = [UNNotificationSound defaultSound]; 7 8// 日付単位でリピートする 9NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; 10NSDateComponents* triggerDate = [[NSCalendar currentCalendar] components:units fromDate:alertDate]; 11UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDate repeats:YES]; 12 13UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:"notifer" content:content trigger:trigger];

###問題点
上記のUNMutableNotificationContentの処理で行うと
int day = 2;で指定した日数経過後に通知する事が出来ません。
その日(既に経過していれば翌日)の指定時間に通知され毎日リピートされます。
unitsにNSCalendarUnitDayを追加すると指定日時に通知させる事は出来ますが
リピート間隔が月単位になってしまいます。

修正方法やアイデアをお持ちの方がいれば、ご教授をお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問