1

注: 以下に更新があります。

前の質問で、1 時間ごとの繰り返し間隔に分を指定することについて尋ねました。ただし、日付コンポーネントを試すように求める 2 つの回答があり、実行しました。ここにあります:

[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];
[componentsForReferenceDate setHour:hour];
[componentsForReferenceDate setMinute:0];
[componentsForReferenceDate setSecond:0];

 NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];
 // Create the notification
 UILocalNotification *notification = [[UILocalNotification alloc] init] ;

 notification.fireDate = fireDateOfNotification;
 notification.timeZone = [NSTimeZone localTimeZone] ;
 notification.alertBody = [NSString stringWithFormat: @"You are missed!"] ;
 notification.alertAction = @"View";
 notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
 notification.repeatInterval= NSHourCalendarUnit ;
 notification.soundName = @"Appnotifisound.wav";
 notification.applicationIconBadgeNumber = 1;
 [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

コードは私には正しいと思われ、毎時間の初めに通知を送信する必要があると言われているように機能するはずです。しかし、そうではありません!コードが完成しました。プロパティを設定し、NSCalendar が存在する場合でも正しく合成しました。それは完璧に構築され、実行されますが、私のシミュレーターでは 1 時間ごとに通知が送信されることはありません!? 間違っていると思われるものを取得できませんでした。さらに情報が必要な場合はお知らせください。または、コードの他の部分をコピーして、不足しているものを見つけるのに役立ててください。ありがとうございました。

更新:これが問題になる可能性がある場合の時間のコーディング方法です..

NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar1 components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:[NSDate date]];
hour = [components hour];
min =[components minute];
sec =[components second];
NSLog(@"hour is %i",hour);

NSLog(@"min is %i",min);

NSLog(@"sec is %i",sec);
 if (hour < 24) {
     hour=hour+1;
     } else {
         hour=0;
4

2 に答える 2

3

私を困惑させたのは、アプリがフォアグラウンドにある間は通知が表示されないことです。シミュレーターのホームボタンを押してアプリをバックグラウンドにするか、アプリがアクティブなときに何かを表示するには、アプリデリゲートプロトコルメソッドを実装し、- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notificationそのメソッドに何かを表示させる必要があります。

于 2013-01-09T23:15:40.623 に答える
0

このコードをコピーすると機能しますが、サウンドを設定せず、「時間」の設定方法を示していないため、ハードコーディングしました。これが出力です。

2013-01-09 17:57:17.968 TestStackOverflow[3322:11303] fireDate = 0001-01-01 06:17:32 +0000 2013-01-09 17:57:17.993 TestStackOverflow[3322:11303] +++scheduledLocalNotifications ( "{発火日 = 1 月 1 日月曜日 1:00:00 AM GMT-05:17:32、タイム ゾーン = アメリカ/トロント (EST) オフセット -18000、繰り返し間隔 = NSHourCalendarUnit、繰り返し回数 = UILocalNotificationInfiniteRepeatCount、次回の起動日 = 2013 年 1 月 9 日水曜日、東部標準時午後 6 時 17 分 32 秒、ユーザー情報 = {\n 情報 = \"一部の情報\";\n}}"

于 2013-01-09T22:59:38.750 に答える