【问题标题】:Local Notification every morning not working?每天早上的本地通知不起作用?
【发布时间】:2014-03-22 17:27:11
【问题描述】:

我想每天早上发送一个本地通知,我每隔applicationDidEnterBackground: 安排一次,然后在applicationWillEnterForeground: 如果安排的通知超过0,我会取消它,问题是我不想使用[app cancelalllocalnotifications];因为有更多的通知计划我不想取消,所以我希望我解释清楚,无论如何这是我的代码:

我将本地通知安排在上午 9:00:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

    user = [self.userdefaults objectForKey:@"user"];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDateComponents *componentsForReferenceDate =
   [calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];

    [componentsForReferenceDate setDay:9] ;
    [componentsForReferenceDate setMonth:11] ;
    [componentsForReferenceDate setYear:2012] ;

    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
    NSDateComponents *componentsForFireDate =

    [calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];

    [componentsForFireDate setHour: 9] ;
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];



    self.diasnot = [[UILocalNotification alloc]init];
    self.diasnot.fireDate = fireDateOfNotification;
    self.diasnot.alertBody = [NSString stringWithFormat: @"Buenos Días %@, tienes %d tareas pendientes. Que tengas buen día!", [self.userdefaults objectForKey:@"user"] ,[[self.userdefaults objectForKey:@"tasks"] count]];

    self.diasnot.timeZone = [NSTimeZone defaultTimeZone];
    if([self.userdefaults objectForKey:@"sound"] == nil)
    {
        self.diasnot.soundName = UILocalNotificationDefaultSoundName;
    }else{
        self.diasnot.soundName = [self.userdefaults objectForKey:@"sound"];
    }


    [application scheduleLocalNotification:self.diasnot];
    [UIApplication sharedApplication].applicationIconBadgeNumber = [[[NSUserDefaults standardUserDefaults] objectForKey:@"tasks"] count];

}

在applicationWillEnterForeground: 我有以下内容:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];

    NSArray *oldNots = [app scheduledLocalNotifications];

    if ([oldNots count] > 0) {
        [app cancelLocalNotification:self.diasnot];
    }


}

问题是,每次我从多任务处理中删除应用程序时,都会显示应用程序,如何在不删除其他通知的情况下解决此问题(这些通知安排在其他类别中,而不是在 AppDelegate 中)谢谢!!

【问题讨论】:

    标签: ios uilocalnotification


    【解决方案1】:

    UILocalNotification 有一个 userInfo 你可以设置为任何你想要的。当您想查找特定通知时,您可以遍历它们并检查 userInfo 的 / 内容的值。

    每次我从多任务处理中删除应用程序时

    您的应用在这样终止时不会收到回调,因此不要依赖于知道它是否发生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多