我想为我的应用设置每日通知系统。它应该每天通知用户两次,一次在上午 8:00(嘿,您的早晨剂量已准备好。想检查一下吗?)和一次在晚上 7:00(嘘!晚上的剂量在里面等着) . 我知道这样做我会在一个月内用完通知,因为有 64 个通知上限(用于本地通知),但到那时,应用程序将上线,我将完成设置远程通知更新。
我看过这个问题:How to schedule a same local notification in swift and the .Day is all good. 我只需要知道如何在指定的时间每天做两次;提前致谢!
编辑:如何在通知上设置具体时间是问题的一部分。NSDate API 给了我有趣的时间间隔(sinceNow/Since1973)。我只想让它在晚上 7 点开火。:-/ 似乎不能用这些 NSDate apis 做到这一点,除非我遗漏了一些东西。
我正在制作我的第一个应用程序,其中一个功能是待办事项列表。我有一个开关,当它关闭时禁用本地通知,当它打开时,它应该在开关打开时启用它们。我使用 保存开关状态UserDefaults,通知确实出现,但是,即使我指定它们应该,它们也不会重复。现在我将通知设置为每 60 秒重复一次以进行测试,但是当它起作用时将需要两天时间。只要开关打开,如何使通知重复?我的开关代码:
@IBOutlet weak var switchout: UISwitch!
@IBAction func notifswitch(_ sender: Any) {
print ("hello")
if switchout.isOn
{
if #available(iOS 10.0, *), list.isEmpty == false {
let content = UNMutableNotificationContent()
content.title = "You have tasks to complete!"
content.subtitle = ""
content.body = "Open the task manager to see which tasks
need completion"
let alarmTime = Date().addingTimeInterval(60)
let components = Calendar.current.dateComponents([.weekday,
.hour, .minute], from: alarmTime)
let trigger = UNCalendarNotificationTrigger(dateMatching:
components, repeats: true)
let …Run Code Online (Sandbox Code Playgroud) 我需要你的一个项目的帮助。
我有 3 个变量,一个用于日,另一个用于月,最后用于年。像那样 :
var year = 2017
var month = 06
var day = 19
即使应用程序在我们处于这些变量的日期时关闭,我也想发送通知,但我对日历和日期不太擅长。我只是暂时制作了这个应用程序。
let myNotification = Notification.Name(rawValue:"MyNotification")
override func viewDidLoad() {
super.viewDidLoad()
let nc = NotificationCenter.default
nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let nc = NotificationCenter.default
nc.post(name:myNotification,
object: nil,
userInfo:["message":"Hello there!", "date":Date()])
}
func catchNotification(notification:Notification) -> Void {
print("Catch notification")
guard let userInfo = notification.userInfo,
let message = userInfo["message"] as? String,
let date = userInfo["date"] as? Date else { …Run Code Online (Sandbox Code Playgroud) 这个问题已经在 SO 上得到了回答。这是参考:
但到目前为止我已经尝试过:
var fortnightPart1 = DateComponents()
fortnightPart1.weekday = Calendar.current.component(.day, from: Sdate) //(Day..here Sunday)
fortnightPart1.weekdayOrdinal = 2 //= n == (nth Day in the month...here 2nd Sunday in every month month)
fortnightPart1.hour = Calendar.current.component(.hour, from: Sdate)
fortnightPart1.minute = Calendar.current.component(.minute, from: Sdate)
fortnightPart1.second = Calendar.current.component(.second, from: Sdate)
trigger = UNCalendarNotificationTrigger(dateMatching: fortnightPart1, repeats: repeate)
Run Code Online (Sandbox Code Playgroud)
我无法在 14 天后实现它。虽然,它来自一个随机数据。任何人都可以检查代码有什么问题吗?
更新:
根据您的建议(/sf/answers/3225074821/),这是最终有效的代码!
//for 1st time notification
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = title
content.body = body …Run Code Online (Sandbox Code Playgroud) calendar uilocalnotification swift3 unnotificationrequest unnotificationtrigger
我有个问题。即使应用程序没有在后台运行,当您从通知打开应用程序时是否可以获取通知正文。它被彻底杀死了。
我有两个问题.
如果我理解本地通知,则重复间隔允许我安排一次通知,并且每周或每月或每周的同一时间间隔重复通知.我想在星期二开始重复一次重复间隔,每周它会在同一天即星期二再次开火.这应该每周进行,而无需安排另一个通知.那是对的吗.是不是发生了.我要么在代码中做错了,要么我测试错了.
在模拟器中,我运行app安排通知.我看到了通知.然后我退出应用程序并在未来的同一天将系统日期设置为1周但没有通知,因此我可以通过更改计算机系统时钟以这种方式测试此通知.我不想每个测试都要等一个星期.
这是代码
- (void) scheduleNotificationWithItem:(NSDate *)date interval:(int)frequency {
UILocalNotification *localNotif = [[UILocalNotification alloc]init];
if (localNotif == nil) {
return;
}
localNotif.fireDate = [date addTimeInterval:frequency];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatCalendar = [NSCalendar currentCalendar];
localNotif.repeatInterval = kCFCalendarUnitWeekday;
localNotif.applicationIconBadgeNumber = 1;
localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@.",nil),@"Weekly Reminder"];
localNotif.alertAction = NSLocalizedString(@"View Notification Details", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotif];
[localNotif release];
}
Run Code Online (Sandbox Code Playgroud)
请帮助这让我发疯.谢谢,迪恩
我有一个应用程序,我需要在一周的某些天发出通知(由用户定义).我之前使用过本地通知,但它总是相当直接,每天,每周等.但是,我从来没有必要同时在特定日期发出相同的通知.有没有人以前这样做过,可以对它有所了解?
我正在提示用户UILocalNotification.当他从通知中启动我的应用时,如果它在后台,我的应用就会变为活动状态.我怎么知道应用程序从通知中被唤醒(活动)?
我只是想知道Local Notification我的应用程序是否可以使用而不是Push Notification.
我的应用程序通过服务器的API从我的服务器获取数据.这是一个适合足球运动员的小型社交网络.我希望应用程序在用户的朋友在用户所在的城市发布游戏时通知用户.
应用程序是否可以运行后台进程来轮询服务器以获取城市中的新拾取游戏,例如每10-15分钟,当有新的拾取游戏时,它会向设备发送通知?
notifications objective-c apple-push-notifications ios uilocalnotification
我正在尝试合并在一定时间间隔后触发的本地通知,但是,在使用模拟器进行测试时,我似乎无法在任何时候显示通知.
我只是想在创建后10秒显示本地通知,但是,当在模拟器中运行应用程序时,无论应用程序是在前台还是后台,都不会显示任何内容.
我错过了什么吗?这可能与时区有关吗?
//通知设置
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = @"Testing";
localNotification.alertAction = @"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Run Code Online (Sandbox Code Playgroud)
//这是我的app委托中的didReceiveLocalNotification
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:notification.alertBody delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
Run Code Online (Sandbox Code Playgroud) timezone uiapplication ios uilocalnotification ios-simulator