OGeek|极客世界-中国程序员成长平台

标题: ios - 如何设置本地通知在(早上 8 :00AM and Evening 8:00PM) in ios? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 19:02
标题: ios - 如何设置本地通知在(早上 8 :00AM and Evening 8:00PM) in ios?

目前我正在使用 Xcode 7.2

我有这样的要求:实际上我想在上午 8:00 和晚上 8:00 安排每日通知(每天仅两次)。

我不熟悉本地通知的使用。我做了一些研发我发现一些链接也喜欢:Daily UILocalNotification firing more than once .但我无法解决我的问题....

如果应用程序处于非事件/事件/后台/暂停,我需要向特定用户显示本地通知。

我该如何克服这个问题?



Best Answer-推荐答案


早上 8:00 的通知

UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
localNotif1.alertBody = @"Morning 8:00 AM";
NSDateComponents *components1 = [[NSCalendar currentCalendar] componentsNSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekOfMonth) fromDate:[NSDate date]];
[components1 setHour:8];
[components1 setMinute:0];
localNotif1.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components1];
[localNotif1 setRepeatInterval:NSCalendarUnitDay];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];

晚上 8:00 的通知

UILocalNotification *localNotif2 = [[UILocalNotification alloc] init];
localNotif2.alertBody = @"Evening 8:00 PM";
NSDateComponents *components2 = [[NSCalendar currentCalendar] componentsNSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekOfMonth) fromDate:[NSDate date]];
[components2 setHour:20];
[components2 setMinute:0];
localNotif2.fireDate = [[NSCalendar currentCalendar] dateFromComponents:components2];
[localNotif2 setRepeatInterval:NSCalendarUnitDay];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif2];

关于ios - 如何设置本地通知在(早上 8 :00AM and Evening 8:00PM) in ios?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36128366/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4