【问题标题】:Local Notification on certain days特定日期的本地通知
【发布时间】:2015-07-28 14:31:58
【问题描述】:

我想在每周三和下午 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]];

}

【问题讨论】:

  • 究竟是什么不起作用?你没有收到通知吗?还是只有一个?

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


【解决方案1】:

创建一个UIDatePicker 只是为了从NSDateComponents 创建一个UIDate 在这里有点奇怪。

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

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

【讨论】:

  • 谢谢,但它仍然不起作用。查看编辑帖子。
  • 那么什么不起作用?添加通知?重复通知?更具体
  • 我将工作日设置为今天 (3),时间设置为 18:00。我关闭了应用程序并等待,没有任何反应:-(
  • 那么当你的 NSDate 添加到 UILocalNotification 时,它实际上有哪个值?
  • 你在哪个时区? NSDate 将始终在调试区域中以 GMT 打印。所以如果你在 CEST f.e.实际上是 +2 小时
猜你喜欢
  • 2019-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多