这是我的代码。我必须为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
}
答案 0 :(得分:7)
来自Local and Remote Notification Programming Guide:
在iOS 8及更高版本中,使用本地或远程通知的应用必须注册他们打算提供的通知类型。然后,系统使用户能够限制应用程序显示的通知类型。如果未对您的应用启用任何这些通知类型,系统不会标记图标,显示警报消息或播放警报声音,即使它们已在通知有效负载中指定。
如指南所示,Apple显示的示例代码为:
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
答案 1 :(得分:2)
Swift的方式是这样的:
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil))
更新: Xcode 7•Swift 2.0
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert , .Sound, .Badge], categories: nil))