iOS 10.2でローカル通知を正常に実装しました。iOS 10.2でローカル通知が行われない
しかし、問題は、アプリがフォアグラウンドである場合は、アプリがバックグラウンドになっても警告が表示されないことです。
フォアグラウンドでローカル通知を受け取ることはできますか?アプリがフォアグラウンドで実行されている場合
私のコードはここに
func notificationNow(){
print("notification will be triggered in five seconds..Hold on tight")
let content = UNMutableNotificationContent()
content.title = "Intro to Notifications"
content.subtitle = "Lets code,Talk is cheap"
content.body = "Sample code from WWDC"
content.sound = UNNotificationSound.default()
//To Present image in notification
if let path = Bundle.main.path(forResource: "menu2", ofType: "png") {
let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "sampleImage", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("attachment not found.")
}
}
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription as Any)
}
}
}
で、ViewControllerを書いていました。私のCustomViewControllerの名前ではなく、唯一の問題です。 –
自分のプロジェクトからコードを追加しました。 "ViewController.swift"内のそのプロジェクト拡張コードで、同じコードがコピーされました。 "ViewController"は、拡張機能が書き込む予定のView Controller名に置き換えてください。 – miOS