gpt4 book ai didi

ios - 发送本地推送通知

转载 作者:行者123 更新时间:2023-11-28 10:24:58 25 4
gpt4 key购买 nike

我正在尝试使用 swift 立即在本地发送推送通知(阅读: future 未安排)。

我在努力

          var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "Some text here"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

已获得发送推送通知的权限。

虽然正在调用代码,但没有发送推送通知。我做错了什么?

谢谢!

最佳答案

你只需要设置 timeZone作为本地时区

The date specified in fireDate is interpreted according to the value of this property. If you specify nil (the default), the fire date is interpreted as an absolute GMT time, which is suitable for cases such as countdown timers. If you assign a valid NSTimeZone object to this property, the fire date is interpreted as a wall-clock time that is automatically adjusted when there are changes in time zones; an example suitable for this case is an an alarm clock.

//应用委托(delegate)

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


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

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Badge, .Sound, .Alert], categories: nil))
return true
}

/// View Controller

import UIKit

class ViewController: UIViewController {
var localNotification = UILocalNotification()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "Some text here"
localNotification.timeZone = NSTimeZone.localTimeZone()

localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

关于ios - 发送本地推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28651760/

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