通知に関しても同様の問題がありました。実際に削除する私の解決策は、特定の日付を設定し、一度だけ通知することでした。行 applicationIconBadgeNumber = 0 は、通知リストから通知を削除します。バッジ番号は、ユーザーがアプリに入ったときにのみ設定されます。ユーザーがまだメッセージを表示していない場合は、applicationWillEnterForeground にチェックを追加して、通知と同じメッセージで適切な UIAlertView を表示するだけで、notificationArray を使用して同様の方法で最後の通知を取得できます。新しい通知を設定する必要がある場合は、"isNotified" に setValue:@"0" を設定して同期する必要があります。複数の通知を使用する場合、次のように状態を保存します。
[userDefaults setValue:@"1" forKey:[NSString stringWithFormat:@"notification-%@", @"12"]];
NSInteger number = [[userDefaults objectForKey:[NSString stringWithFormat:@"notification-%@", @"12"]] integerValue];
MainViewController.m 内
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger alreadyNotified = [[userDefaults objectForKey:@"isNotified"] integerValue];
if (alreadyNotified == 0) {
NSInteger seconds = 60;
NSString *message = @"Just testing!";
UILocalNotification *notification = [[UILocalNotification alloc] init];
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
BOOL allowNotif = (currentSettings.types != UIUserNotificationTypeNone);
BOOL allowsSound = (currentSettings.types & UIUserNotificationTypeSound) != 0;
BOOL allowsBadge = (currentSettings.types & UIUserNotificationTypeBadge) != 0;
BOOL allowsAlert = (currentSettings.types & UIUserNotificationTypeAlert) != 0;
if (notification)
{
if (allowNotif)
{
NSDate *now = [NSDate date];
if (seconds > 0) {
now = [now dateByAddingTimeInterval:seconds];
}
notification.fireDate = now;
notification.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
notification.repeatInterval = 0;
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"id", message, @"message", nil];
notification.userInfo = userInfo;
}
if (allowsAlert)
{
notification.alertBody = message;
}
if (allowsBadge)
{
notification.applicationIconBadgeNumber = 1;
}
if (allowsSound)
{
notification.soundName = UILocalNotificationDefaultSoundName;
}
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:@"1" forKey:@"isNotified"];
[userDefaults synchronize];
}
} else {
UIApplication *app = [UIApplication sharedApplication];
NSArray *notificationArray = [app scheduledLocalNotifications];
for (int i=0; i<[notificationArray count]; i++)
{
UILocalNotification *notification = [notificationArray objectAtIndex:i];
[app cancelLocalNotification:notification];
}
}
AppDelegate.m 内
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}