我iOS 10
開發local Notifications
。它工作完美。但是,現在應該如何,如果用戶使用的是iOS 9
及以上版本的I CODE local notifications
和push notification
。任何人都可以幫忙嗎?本地和推送通知和10使用swift3
下面是iOS 10
import UIKit
import UserNotifications
@available(iOS 10.0, *)
class ViewController: UIViewController,UNUserNotificationCenterDelegate {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 10.0, *) {
//Seeking permission of the user to display app notifications
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: {didAllow,Error in })
UNUserNotificationCenter.current().delegate = self
}
}
//To display notifications when app is running inforeground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound, .badge])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(_ sender: UIButton) {
if #available(iOS 10.0, *) {
//Setting content of the notification
let content = UNMutableNotificationContent()
content.title = "hello"
content.body = "notification pooped out"
content.badge = 1
//Setting time for notification trigger
let date = Date(timeIntervalSinceNow: 10)
var dateCompenents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateCompenents, repeats: false)
//Adding Request
let request = UNNotificationRequest(identifier: "timerdone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
}
你的問題是什麼?你有什麼嘗試? – shallowThought
我需要修改我的代碼,以使它也可以在iOS9中運行。我在這裏使用的UserNotifications框架僅存在於iOS 10 –
如何設置不同的日期組件或如何多次調用本地通知? – ArgaPK