本地通知"每天上午 7:00"未通知



我希望每天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];
}

尝试使用以下方式:

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

您需要致电

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

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

即使这是一个非常古老的问题,我刚从Google来到这里,是的,不是每个人都通过复制其他人的回答来回答吗?

因此,对于所有来自Google的人,都会笑一个拼写错误,螨虫花了一两分钟,想知道为什么它不起作用。

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

有效,但是

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

通知不会...

它正在工作

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

相关内容

  • 没有找到相关文章

最新更新