这是我的代码。我必须为 localNotification 添加什么吗?有什么建议。提前致谢。我阅读了很多本地通知教程。我不知道错过了什么。
var localNotifications : UILocalNotification!
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
localNotifications = UILocalNotification()
localNotifications.fireDate = NSDate(timeIntervalSinceNow: 20)
localNotifications.timeZone = NSTimeZone.defaultTimeZone()
localNotifications.alertBody = "Check it out!"
localNotifications.soundName = UILocalNotificationDefaultSoundName
localNotifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
UIApplication.sharedApplication().scheduleLocalNotification(localNotifications)
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
println("Alvin Notification recieved")
if application.applicationState == UIApplicationState.Active
{
// show alertView
Utilities.sharedInstance.alertThis("Hello", message: "Hey")
}
else if application.applicationState == UIApplicationState.Background
{
UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
}
else if application.applicationState == UIApplicationState.Inactive
{
UIApplication.sharedApplication().presentLocalNotificationNow(localNotifications)
}
application.applicationIconBadgeNumber = 0
}
最佳答案
来自Local and Remote Notification Programming Guide :
In iOS 8 and later, apps that use either local or remote notifications must register the types of notifications they intend to deliver. The system then gives the user the ability to limit the types of notifications your app displays. The system does not badge icons, display alert messages, or play alert sounds if any of these notification types are not enabled for your app, even if they are specified in the notification payload.
如指南所示,Apple 展示的示例代码是:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
关于ios - UILocalNotification 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376233/