我做了一個應用程序,在您點擊一個按鈕後的某段時間後發送通知。此通知是在ViewController中創建的。在用戶點擊通知後,如何讓我的應用程序執行某些操作?我正在使用swift 3而不使用UILocalNotification。(ios10.2)(swift3)如何判斷用戶是否點擊了本地通知?
0
A
回答
1
在您的應用程序委託中,將對象配置爲用戶通知中心的UNUserNotificationCenterDelegate並執行userNotificationCenter(_:didReceive:withCompletionHandler:)
。
需要注意的是,如果用戶僅僅駁回通知提醒,這種方法會不會被稱爲除非您還配置對應於該通知的類別(UNNotificationCategory),與.customDismissAction
選項。
1
UILocalNotification在iOS 10中已棄用。您應該改爲使用UserNotifications框架。
所有此之前不要忘import UserNotifications
首先,你應該建立UNUserNotificationCenterDelegate
。
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
// - Handle notification
}
}
比設置UNUserNotificationCenter
的代表。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (accepted, _) in
if !accepted {
print("Notification access denied.")
}
}
return true
}
現在您可以設置您的通知。
func setup(at: Date) {
let calendar = Calendar.current
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current,
month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "Just a reminder"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
最後處理它!
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
if response.notification.request.identifier == "textNotification" {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
guard let rootVc = appDelegate.window?.rootViewController else { return }
let alert = UIAlertController(title: "Notification", message: "It's my notification", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alert.addAction(action)
rootVc.present(alert, animated: true, completion: nil)
}
}
}
相關問題
- 1. 如何判斷是否在Rails中讀取了通知
- 2. 檢測用戶點擊本地通知
- 3. 如何判斷用戶是否使用iOS 7遠程通知後臺模式通知了應用程序?
- 4. 如何判斷用戶是否離線
- 5. 如何判斷用戶是否啓用了「話語提示」?
- 6. 如何判斷是否安裝了node.js
- 7. 如何知道點擊通知後是否打開了活動?
- 8. 如何快速判斷用戶是否觸碰了許多CGRects
- 9. 如何判斷用戶是否輸入了字符串?
- 10. 如何知道用戶是否點擊了表格中的X
- 11. 如何知道用戶是否點擊了微調控件android
- 12. 如何知道用戶是否點擊了視頻播放
- 13. 如何判斷當應用程序在後臺時是否觸發了本地通知?
- 14. Android - 如何判斷用戶是否在TextView以外的任何位置點擊?
- 15. 如何判斷editText是否在焦點?
- 16. 如何處理用戶點擊的本地通知
- 17. 活動目錄通知 - 如何判斷更改是否爲新用戶
- 18. 如何判斷用戶是否在從Java servlet下載期間點擊取消?
- 19. 如何判斷收到哪些本地通知? Objective - C的
- 20. 如何判斷用戶是否投票通過鏈接?
- 21. 如何判斷當前是否選擇了Google地圖標記?
- 22. 如何判斷用戶是否在我的頁面上選擇了文本
- 23. 如何判斷我是否已經處理了一個節點
- 24. Selenium IDE:如何判斷用戶是否在腳本提示中單擊取消?
- 25. Xamarin格式的NotificationHub:如何判斷通知是否已發送
- 26. 如何判斷是否在android中下拉通知窗口
- 27. 如何判斷通知是否可以發送給Apple Watch?
- 28. 如何判斷意圖是否來自Google Cast通知
- 29. 如何判斷按鈕是否在android中未被點擊?
- 30. 如何判斷一個複選框是否實際被點擊?
通過不使用UILocalNotification,你的意思是你使用UNNotification? – Simon