【问题标题】:Local Notification "Everyday at 7:00am" not notifying本地通知“每天早上 7:00”未通知
【发布时间】:2012-11-08 19:21:20
【问题描述】:

我希望每天 7:00 发出通知,但它不会发出。我也希望它显示在锁定屏幕中。这是我到目前为止的所有代码。

-(void)EveryDayNotify

{

    NSLog(@"Good Morning Working");

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


    [localNotification setAlertBody:@"Good Morning! Have a great day!"];
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber = 1;

    localNotification.repeatInterval = NSDayCalendarUnit;

    NSCalendar *calendar = [NSCalendar currentCalendar]; // gets default calendar
    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; // gets the year, month, day,hour and minutesfor today's date
    [components setHour:07];
    [components setMinute:0];

    localNotification.fireDate = [calendar dateFromComponents:components];


    [localNotification release];
}

【问题讨论】:

    标签: iphone ios xcode ipad uilocalnotification


    【解决方案1】:

    尝试使用这个:

    - (void)applicationDidEnterBackground:(UIApplication *)application 
    {
    
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    
    NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
    
    [componentsForReferenceDate setDay:9];
    [componentsForReferenceDate setMonth:11];
    [componentsForReferenceDate setYear:2012];
    
    NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
    
    // set components for time 7:00 a.m.
    
    NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
    
    [componentsForFireDate setHour:7];
    [componentsForFireDate setMinute:0];
    [componentsForFireDate setSecond:0];
    
    NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
    
    // Create the notification
    
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    
    notification.fireDate = fireDateOfNotification;
    notification.timeZone = [NSTimeZone localTimeZone];
    notification.alertBody = [NSString stringWithFormat: @"Good Morning! Have a great day!"];
    notification.alertAction = @"go back";
    notification.userInfo= @{@"information": [NSString stringWithFormat:@"Some information"]};
    notification.repeatInterval= NSCalendarUnitDay;
    notification.soundName = UILocalNotificationDefaultSoundName;
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    
    }  
    

    如果有帮助,请点赞! :D

    【讨论】:

    • componentsForReferenceDate 是什么?
    • 好吧,让我们说你的 setMonth: 11 意味着在 11 月调用了 fireate,对于其他人来说,同样的事情我已经为你设置了代码,这就是你想要的
    • 好的,现在只需将整个代码复制并粘贴到您的应用委托中
    • 所以我不应该担心 componentsForReferenceDate 吗?
    • 只需将 NSDaycalenderunit 设置为 NSMinuteCalenderunit,它就会每分钟和你设置的秒数弹出
    【解决方案2】:

    你需要打电话

    [[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
    

    在您发布 localNotification 之前。您的代码完成了所有设置,但实际上并没有安排它,这就是为什么您永远不会得到它。

    【讨论】:

      【解决方案3】:

      尽管这是一个非常古老的问题,但我刚从谷歌来到这里,是的,每个人都通过复制其他人的答案来回答这不是很有趣吗?

      因此,对于像我这样来自 google 的所有人,请对拼写错误一笑置之,因为 mite 让您一两分钟都想知道为什么它不起作用。

      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
      

      有效,但是

      [[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];
      

      通知不...

      【讨论】:

        【解决方案4】:

        它正在工作

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
                // Override point for customization after application launch.
        
                NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
        
                NSDateComponents *componentsForReferenceDate = [calendar components:(NSCalendarUnitDay | NSCalendarUnitYear | NSCalendarUnitMonth ) fromDate:[NSDate date]];
        
                [[UIApplication sharedApplication] cancelAllLocalNotifications];
        
                [componentsForReferenceDate setDay:27];
                [componentsForReferenceDate setMonth:10];
                [componentsForReferenceDate setYear:2016];
        
                NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate];
        
                // set components for time 7:00 a.m.
        
                NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond ) fromDate: referenceDate];
        
                [componentsForFireDate setHour:8];
                [componentsForFireDate setMinute:0];
                [componentsForFireDate setSecond:0];
        
        
                NSDate *fireDateOfNotification = [calendar dateFromComponents:componentsForFireDate];
        
                // Create the notification
        
                UILocalNotification *notification = [[UILocalNotification alloc] init];
        
                notification.fireDate = fireDateOfNotification;
                notification.timeZone = [NSTimeZone localTimeZone];
                notification.alertBody = [NSString stringWithFormat: @"БЛАГОСЛОВИТЕ ПРОРОКА (с.а.в)"];
                notification.repeatInterval= NSCalendarUnitDay;
                notification.soundName = UILocalNotificationDefaultSoundName;
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 1];
                [[UIApplication sharedApplication] scheduleLocalNotification:notification];
                [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
        
                return YES;
        
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多