我正在使用Swift 3编码,尝试发送立即通知而没有任何延迟或间隔。然而,通知从未被触发。这是我的代码:
ViewController代码
import UserNotifications
class HomeViewController: UIViewController{
var isGrantedNotificationAccess:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedNotificationAccess = granted
})
if isGrantedNotificationAccess{
triggerNotification()
}
}
//triggerNotification func goes here
}
触发通知函数:
func triggerNotification(){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 1.0,
repeats: false)
let request = UNNotificationRequest.init(identifier: "testTriggerNotif", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
我做错了什么?