gpt4 book ai didi

ios - UILocalNotification 不发送

转载 作者:行者123 更新时间:2023-11-30 13:14:24 27 4
gpt4 key购买 nike

我正在尝试发送如下所示的 UILocalNotification:

func sendNotifiaction() {
let notification = UILocalNotification()
notification.userInfo = [
"Identifier": self.identifier!
]
notification.alertTitle = "Alarm!"
notification.alertBody = "test"
//notification.soundName = "Temporary-bleep-sound.aiff"
notification.category = "category"

UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

我尝试在此方法上放置一个断点,它正在被调用并运行,但通知根本没有发送。

有人知道为什么吗?

最佳答案

您必须先注册通知

UIApplication.sharedApplication().cancelAllLocalNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

这是我在特定时间触发通知的演示

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

UIApplication.sharedApplication().cancelAllLocalNotifications()
let settings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)

let localNotification1 = UILocalNotification()
localNotification1.alertBody = "Your alert message at 9:00 pm"
localNotification1.timeZone = NSTimeZone.defaultTimeZone()
localNotification1.fireDate = self.getNinePMDate()
UIApplication.sharedApplication().scheduleLocalNotification(localNotification1)
return true
}

func getNinePMDate() -> NSDate? {
let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let now: NSDate! = NSDate()
let date21h = calendar.dateBySettingHour(21, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)
return date21h
}
}

关于ios - UILocalNotification 不发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392946/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com