特定日期的本地通知

时间:2015-07-28 14:31:59

标签: objective-c notifications push-notification apple-push-notifications

我想在每周三和下午1点重复本地通知消息。

但这段代码的错误是什么?

修改

- (void)applicationDidEnterBackground:(UIApplication *)application {
//Calendar
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];

[dateCompnent setHour:18];
[dateCompnent setMinute:03];
[dateCompnent setWeekday:3];

NSDate *date = [gregCalendar dateFromComponents:dateCompnent];

//Notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setAlertBody:@"Test Notification"];
[notification setFireDate:date];
[notification setSoundName:@"sound.mp3"];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

}

1 个答案:

答案 0 :(得分:0)

仅从UIDatePicker创建UIDate来创建NSDateComponents在这里有点奇怪。

您可以使用dateFromComponents中的NSCalendar方法:

NSDate *date = [gregCalendar dateFromComponents:dateCompnent];

另外,请尝试将NSDateComponents更改为仅使用的组件:

NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];

[dateCompnent setWeekday:4];
[dateCompnent setHour:13];
[dateCompnent setMinute:00];

修改

不确定哪些是有效的,但我的猜测是重复(因为我没有看到任何与重复UILocalNotification相关的代码)

查看UILocalNotification的类引用,更具体地说,查看属性repeatInterval