1

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

4 回答 4

7

尝试使用这个:

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

于 2012-11-08T19:32:30.790 回答
2

你需要打电话

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

在你释放之前localNotification。您的代码完成了所有设置,但实际上并没有安排它,这就是您永远不会得到它的原因。

于 2012-11-08T19:25:05.397 回答
0

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

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

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

有效,但是

[[UIApplication sharedApplication] scheduleLocalNotiication:localNotification];

通知不...

于 2015-05-19T08:41:54.603 回答
0

它正在工作

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

    }
于 2016-10-30T07:16:23.740 回答