首页 文章

检查是否允许本地通知

提问于
浏览
1

我每天都在同一时间重复本地通知 .

现在一切正常, except when Notifications are toggled OFF, then back ON, there is a build up of notifications from the days it was OFF.

有没有最好的方法来检查是否切换为OFF然后重新打开,并清除所有旧的通知?

谢谢!

- (void)viewDidLoad {
    [super viewDidLoad];

    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];

    // before we create any new ones, cancel all existing notifications
    [[UIApplication sharedApplication]cancelAllLocalNotifications];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
    comp.hour = 19; // 19 = 7PM
    comp.minute = 45; // 7:45 PM
    comp.second = 01; // 7:45:01 PM

    localNotification.fireDate = [cal dateFromComponents:comp];
    localNotification.alertBody = @"Local Notification in iOS8";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = NSCalendarUnitDay;

    // this will schedule the notification to fire at the fire date
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    // this will fire the notification right away, it will still also fire at the date we set
    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}

2 回答

  • -2

    在提交通知之前,检查用户设置是否允许:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    
        // before we create any new ones, cancel all existing notifications
        [[UIApplication sharedApplication]cancelAllLocalNotifications];
    
        if ([[[UIApplication sharedApplication] currentUserNotificationSettings] types] != UIUserNotificationTypeNone) {
    
            UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    
            NSCalendar *cal = [NSCalendar currentCalendar];
            NSDateComponents *comp = [cal components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
            comp.hour = 19; // 19 = 7PM
            comp.minute = 45; // 7:45 PM
            comp.second = 01; // 7:45:01 PM
    
            localNotification.fireDate = [cal dateFromComponents:comp];
            localNotification.alertBody = @"Local Notification in iOS8";
            localNotification.timeZone = [NSTimeZone defaultTimeZone];
            localNotification.repeatInterval = NSCalendarUnitDay;
    
            // this will schedule the notification to fire at the fire date
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
            // this will fire the notification right away, it will still also fire at the date we set
            [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    
        }
    }
    
  • 1

    在事件切换时,请尝试以下操作 -

    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    

相关问题