Ios 发送本地推送通知
我正在尝试使用swift立即在本地发送推送通知(阅读:未计划在将来) 我正在努力Ios 发送本地推送通知,ios,swift,push-notification,Ios,Swift,Push Notification,我正在尝试使用swift立即在本地发送推送通知(阅读:未计划在将来) 我正在努力 var localNotification:UILocalNotification = UILocalNotification() localNotification.alertAction = "Testing notifications on iOS8" localNotification.alertBody = "Some text here"
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "Testing notifications on iOS8"
localNotification.alertBody = "Some text here"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
已授予发送推送通知的权限
虽然调用了代码,但不会发送推送通知。我做错了什么
谢谢 您必须确保为您的应用注册通知(例如在
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
然后您的代码将被执行
另一件事是,如果您在前台使用应用程序,您可能根本看不到它们-请尝试通过向下滑动打开通知中心。您必须确保为您的应用程序注册通知(例如在
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
然后您的代码将被执行
另一件事是,如果你在前台使用应用程序,你可能根本看不到它们-尝试通过向下滑动打开
通知中心。你的问题可能是你看不到通知,但它实际上就在那里。只需打开通知中心,它就应该在那里:
要检查这一点,请在AppDelegate中实现didReceiveLocalNotification
方法:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
println("Notification done")
}
然后,您可以处理通知并显示一个弹出窗口或任何您想要的内容。您的问题可能是您没有看到通知,但它实际上就在那里。只需打开通知中心,它就应该在那里:
要检查这一点,请在AppDelegate中实现didReceiveLocalNotification
方法:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
println("Notification done")
}
然后,您可以处理通知并显示弹出窗口或任何您想要的内容。您只需将设置为localTimeZone即可
fireDate中指定的日期根据值进行解释
这一财产的所有权。如果指定nil(默认值),则火灾日期为
解释为绝对GMT时间,适用于此类情况
作为倒计时。如果将有效的NSTimeZone对象指定给此
属性,则火灾日期被解释为
时区发生变化时自动调整;一
适用于这种情况的示例是闹钟
//应用程序代理
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.
}
}
您只需将设置为localTimeZone
fireDate中指定的日期根据值进行解释
这一财产的所有权。如果指定nil(默认值),则火灾日期为
解释为绝对GMT时间,适用于此类情况
作为倒计时。如果将有效的NSTimeZone对象指定给此
属性,则火灾日期被解释为
时区发生变化时自动调整;一
适用于这种情况的示例是闹钟
//应用程序代理
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.
}
}
我认为你的问题是,你只是看不到答案。(查看我的答案了解更多信息)我认为你的问题是,你只是看不到答案。(查看我的答案了解更多信息)