花了兩天追完了WWDC2016 [707] & [708],算是對推播有稍微地認識了。
今天將會延續Day32的專案,繼續增加特定日期的推播功能。
現在,我們馬上開始!
設定在畫面的水平置中對齊
距離按鈕往下30
與按鈕等寬
@IBAction func catSwitchChanged(_ sender: UISwitch) {
if sender.isOn {
let content = GetNotificationContent()
var date = DateComponents()
date.day = 8
date.hour = 8
date.minute = 5
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "catMonthNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else {
// remove notifications
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["catMonthNotification"])
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["catMonthNotification"])
}
}
首先從UISwitch.isOn取得狀態,是的話就訂閱通知,否的話就取消訂閱通知
『台灣石虎保育協會』的誕生日2017/10/8,因此程式碼設定為每月的8號作為石虎關心日
將repeats設定為true,則每月8號的0830都會收到關心石虎的推播通知
程式碼與catButtonPressed有重複代碼,GetNotificationContent()方法抽離出去
private func GetNotificationContent() -> UNMutableNotificationContent {
let content = UNMutableNotificationContent()
content.title = "守護石虎人人有責"
content.subtitle = "石虎抱抱"
content.body = """
向親朋好友介紹石虎,讓他們更喜愛這種動物
向親朋好友介紹石虎面臨的困境
行車時若看見小心石虎的標誌請減速慢行
(苗栗縣相關標誌類似附圖)
不使用捕獸鋏、避免使用農藥和有毒餌劑
不任意棄養寵物、不任意獵殺野生動物
"""
content.badge = 3
content.sound = UNNotificationSound.default
content.categoryIdentifier = "catMessage"
let imageURL = Bundle.main.url(forResource: "Cat2", withExtension: "jpg")
let attchment = try! UNNotificationAttachment(identifier: "catNotification1", url:
imageURL!, options: nil)
content.attachments = [attchment]
content.userInfo =
["link":"https://img5.cna.co
.tw/www/WebPhotos/1024/20190828/960x960_268359289092.jpg"]
return content
}
今天的練習比較簡單,增加了一個日期訂閱的觸發器(UNCalendarNotificationTrigger),在指定的日期與時間,發送推播通知。而更精準的設定可以參考Calendar(ref12 & ref13),一般日期都會與DatePicker的UI元件做搭配使用。
今天的文章就到這邊,感謝讀者的閱讀。
https://github.com/chiron-wang/IT30_11
彼得潘的 Swift iOS App 開發問題解答集
https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86
iOS 13 & Swift 5 - The Complete iOS App Development Bootcamp - Angela Yu
https://www.udemy.com/course/ios-13-app-development-bootcamp/
深入淺出 iPhone 開發 (使用 Swift4) - WeiWei
https://www.udemy.com/course/iphone-swift4/
心智圖軟體Xmind
https://www.xmind.net/
Apple WWDC2016 707
https://developer.apple.com/videos/play/wwdc2016/707
Apple WWDC2016 708
https://developer.apple.com/videos/play/wwdc2016/708
俄羅斯插畫家親繪石虎送台灣 有望登上彩繪列車!
https://udn.com/news/story/7266/4013861
台灣石虎保育協會
https://www.facebook.com/pg/LCAT2017/about/?ref=page_internal
Apple doc UNCalendarNotificationTrigger
https://developer.apple.com/documentation/usernotifications/uncalendarnotificationtrigger
Apple doc UserNotifications
https://developer.apple.com/documentation/usernotifications
【iOS 10 - 本地通知(Local Notification)】
https://medium.com/@mikru168/ios-%E6%9C%AC%E5%9C%B0%E9%80%9A%E7%9F%A5-local-notification-b25229f279ec
Apple doc Calendar
https://developer.apple.com/documentation/foundation/calendar