带有动态警报主体的快速本地通知 [英] Swift local notification with dynamic alert body

查看:78
本文介绍了带有动态警报主体的快速本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我可以创建这样的本地通知

So I can create a local notification like this

    var localNotification = UILocalNotification()
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 7)
    localNotification.alertBody = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .MediumStyle, timeStyle: .ShortStyle)
    localNotification.timeZone = NSTimeZone.defaultTimeZone()
    localNotification.repeatInterval = NSCalendarUnit.CalendarUnitMinute
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

这会立即创建一个带有日期的通知,然后开始每分钟触发一次,直到永恒.如果创建通知的时间是下午5点,则每次触发时都会说下午5点,即使30分钟后的时间是5:30.

This creates a notification with the date right now and then starts firing it off every minute for eternity. If the time the notification was created is 5pm it will say 5pm every time it fires, even if 30 minutes later the time is 5:30.

我该怎么做才能使警报主体动态化?

What do I need to do to make the alert body dynamic?

我在文档中阅读了它期望为alertBody的字符串,所以我在尝试弄清楚如何使用函数调用,时间戳或更改的值来设置消息正文时遇到了麻烦.

I read in the docs it expects a string for alertBody so I am having trouble trying to figure out how to set the message body with a function call or a time stamp or a value that changes.

https ://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertBody

基本上,我希望我的应用在创建通知时(而不是在设置通知时)计算一个值来创建消息.关于示例,每当发生提示时都会使用一个通知,因此您可以在运行中看到此代码.在实际应用中,我想在预定的时间触发每日通知. EG每天早上7点执行一次,然后计算该时间的值.

Basically I want my app to calculate a value at the time the notification is about to fire, not at the time it is set, to create the message. The example about uses a notification every minute for symplicty so you can see this code in action. In the real app I want to fire a daily notification at a pre determined time. EG Once a day at 7am and calculate a value at that time.

推荐答案

您不能使其动态化.对于每个单独的时间,您将需要带有单独文本的单独通知.

You can't make it dynamic. You would need separate notifications with separate text for each of the separate times.

这篇关于带有动态警报主体的快速本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆