2

ユーザーに毎日午前 9 時に通知を送信するアプリを作成しましたが、ユーザーが必要に応じてオフにできるようにしたいと考えています。

アプリの設定バンドルをセットアップし、enableNotifications のスライダーをセットアップしました。アプリをビルドしてテストすると、設定があり、オンまたはオフにすることができます...しかし、アプリに登録していないようです。通知設定がオンかオフかを確認するようにアプリをプログラムするにはどうすればよいですか?

私はこの答えを見ました:ユーザーがプッシュ通知を有効にしているかどうかを iPhone で判断しますが 、どこにそれを入れて適切にプログラムすればよいかわかりません (if/else ステートメントなど)。

通知が作成されるコードは次のとおりです。

-(id) getCommandInstance:(NSString*)className
{
/** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
* own app specific protocol to it. -jm
**/
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];

NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate:currentDate];

NSDateComponents *temp = [[NSDateComponents alloc]init];

[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour: 22];
[temp setMinute:00];

NSDate *fireTime = [calender dateFromComponents:temp];
[temp release];

// set up the notifier
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = fireTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];

localNotification.alertBody = @"Don't Forget Your 365 Day Photo Challenge!";
localNotification.alertAction = @"LAUNCH";

localNotification.soundName = UILocalNotificationDefaultSoundName;

localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[localNotification release];
return [super getCommandInstance:className];
}
4

2 に答える 2

1

localNotificationが1つだけあり、それをキャンセルしたい場合は、次の電話番号に電話してください。

[[UIApplication sharedApplication] cancelAllLocalNotifications];

localNotificationsがない場合、これはエラーをスローしません。これは、ボタンを押してIBActionを押すことで呼び出すことができます。localNotificationsがあるかどうかに基づいてスイッチの状態を設定する場合は、次のいずれかがあるかどうかを確認できます。

NSArray* localNotifications = [[UIApplication sharedApplication] 
                                              scheduledLocalNotifications];

localNotifications.count> 0の場合、いくつかの通知がスケジュールされています。

于 2012-08-24T22:46:51.490 に答える
0

あなたの説明に基づいて、アプリの設定から通知設定の値を取得する方法、つまり通知を有効にするか無効にするかを尋ねていると思います。

設定値にアクセスするには、[NSUserDefaults standardUserDefaults] を使用する必要があります。このAccessing Preference Valuesドキュメントを参照してください。

于 2012-08-26T05:21:26.337 に答える