【问题标题】:Don't send daily Local Notification if app already opened如果应用程序已经打开,不要每天发送本地通知
【发布时间】:2015-06-03 20:23:17
【问题描述】:

我每天都在同一时间发送Local Notification

我想知道是否有办法如果用户当天已经打开应用程序,则不发送通知

这是我用来检查是否允许通知,然后发送通知的代码:

- (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];

    }
}

编辑:这是我尝试按照以下答案实施方法建议的代码?不过,我显然在这里没有得到任何东西:/

- (void)isDateLowerThanToday {
    NSDate *now = [NSDate date];
    int daysToAdd = -1;
    NSDate *newDate1 = [now dateByAddingTimeInterval:60*60*24*daysToAdd];
    NSLog(@"NewDate1 = %@", newDate1);
}

【问题讨论】:

    标签: ios objective-c notifications uilocalnotification


    【解决方案1】:

    您可以将上次通知发送日期存储在NSUserDefaults。然后,当应用程序打开时,您检查此值,如果它为 null 或低于 Today,您将发送通知并将 NSUserDefaults 上的值更新为当前日期。

    这样的事情应该可以解决问题

    NSString *key = @"LAST_NOTIFICATION_SENT_DATE";
    NSDate *date = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    
    // You have to implement isDateLowerThanToday
    if (!date || [self isDateLowerThanToday:date]) { 
        [self sendNotification];
        date = [NSDate date];
        [[NUserDefaults standardUserDefaults] setObject:date forKey:key];
    }
    

    这里有isDateLowerThanToday 实现

    -(bool)isDateLowerThanToday:(NSDate *)date {
        NSDate * now = [NSDate date];
        NSComparisonResult result = [now compare:date];
    
        return result == NSOrderedDescending;
    }   
    

    【讨论】:

    • 甜我要试试,有道理!
    • [self sendNotification]; 是我已有的发送通知的代码的占位符吗?
    • 我知道我应该实现isDateLowerThanToday,但是该方法中的代码是否找到了今天的日期?我想我很难弄清楚如何使用这种方法?你能帮我解决这个问题吗?谢谢!
    • 我添加了我能想到的最好的东西作为我的答案的编辑,你怎么看?
    • @SRMR:抱歉耽搁了,添加了实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    相关资源
    最近更新 更多