我正在尝试使用swift,immediatelly在本地发送推送通知(读取:未安排在将来)。
我正在尝试
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "Some text here"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
有权发送已经授予的推送通知。
虽然代码被调用,但没有发送推送通知。我做错了什么?
谢谢!
答案 0 :(得分:2)
您只需将timeZone设置为localTimeZone
即可根据值解释fireDate中指定的日期 这个属性。如果指定nil(默认值),则触发日期为 解释为绝对GMT时间,适用于此类情况 作为倒数计时器。如果为此分配有效的NSTimeZone对象 属性,起火日期被解释为挂钟时间 当时区发生变化时自动调整;一个 适合这种情况的例子是一个闹钟。
// App委托
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
}
///查看控制器
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.
}
}
答案 1 :(得分:1)
您已确保为您的应用注册通知(例如在AppDelegate
中)。
在Objective-C中,我设法使用:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
#else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif
然后你的代码就会被执行。
另一件事是,如果您在前台使用应用,则可能根本看不到它们 - 请尝试通过向下滑动来打开Notification Center
。
答案 2 :(得分:1)
您的问题可能是您没有看到通知,但它确实存在。只需打开通知中心即可:
要检查这一点,请在AppDelegate中实施didReceiveLocalNotification
方法:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
println("Notification done")
}
然后,您可以处理通知并显示PopUp或任何您想要的内容。