简体   繁体   English

本地通知不适用于iOS模拟器

[英]Local notification not working with iOS simulator

I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 I have the following code: 我有以下代码:

//Create date
let calendar = NSCalendar.currentCalendar()
let now = NSDate()

let dateComponents = calendar.components([NSCalendarUnit.Day, NSCalendarUnit.Month, NSCalendarUnit.Year, NSCalendarUnit.Hour, NSCalendarUnit.Minute, NSCalendarUnit.Second], fromDate: now)

dateComponents.minute += 2 //add 2 minutes to current time for debug

let date = calendar.dateFromComponents(dateComponents)

//Create notification
let notification = UILocalNotification()
notification.alertBody = "Your task is now available"
notification.alertAction = "open"
notification.fireDate = date
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["id": NSUUID().UUIDString]
notification.category = "Notification"
     UIApplication.sharedApplication().scheduleLocalNotification(notification)

When I place a breakpoint in application: didReceiveLocalNotification in app delegate, it never gets called. 当我在应用程序中放置一个断点时:在应用程序委托中执行didReceiveLocalNotification,就永远不会调用它。

Ok, I'll answer my own question. 好吧,我会回答我自己的问题。 In AppDelegate: didFinishLaunchingWithOptions, I had to register notifications like so: 在AppDelegate:didFinishLaunchingWithOptions中,我必须这样注册通知:

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(settings)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 本地通知无法正常运行iOS 8 - Local Notification not working iOS 8 如何在 iOS 模拟器中模拟本地/推送通知? - How to simulate a local/push notification in iOS simulator? iOS本地通知声音不起作用 - iOS Local Notification Sound Not Working 本地通知在ios中无法正常运行 - Local Notification is not working well in ios 本地通知在iOS 10.3.1中无效 - Local Notification not working in iOS 10.3.1 本地通知可在ios模拟器上运行,但不能在设备上运行 - Local notifications working on ios simulator but not on device 在PhoneGap / Cordova中安排本地通知在iOS模拟器上不起作用 - Scheduling a local notification in PhoneGap/Cordova doesn't work on iOS simulator 在模拟器或物理设备上都无法在IOS中获得本地通知 - Not getting local notification in IOS neither on simulator nor on physical device iOS Firebase 推送通知不适用于 Xcode 11.5 和 iOS 13 模拟器 - iOS Firebase Push notification not working on simulator with Xcode 11.5 and iOS 13 cordova 本地通知声音在 ios 和 Android 中不起作用 - cordova local notification sound not working in ios and Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM