iT邦幫忙

第 11 屆 iThome 鐵人賽

0
Software Development

iOS App 實作開發新手村系列 第 34

Day34 石虎的推播通知 (石虎有約)

20191020

前言

花了兩天追完了WWDC2016 [707] & [708],算是對推播有稍微地認識了。

今天將會延續Day32的專案,繼續增加特定日期的推播功能。

Day32
https://ithelp.ithome.com.tw/articles/10228620

現在,我們馬上開始!

練習過程

  1. 首先加入UILabel & UISwitch兩個元件,並把他包在StackView內

  1. StackView的約束設定

  • 設定在畫面的水平置中對齊

  • 距離按鈕往下30

  • 與按鈕等寬

  1. 將UISwitch與程式碼做連結

  • 由於IBAction的sender就可以取得UISwitch的狀態,因此,Outlet就不做設定
  1. 加入以下程式碼
@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元件做搭配使用。

今天的文章就到這邊,感謝讀者的閱讀。


Github:

https://github.com/chiron-wang/IT30_11

參考資料與延伸閱讀

  1. 彼得潘的 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

  2. iOS 13 & Swift 5 - The Complete iOS App Development Bootcamp - Angela Yu
    https://www.udemy.com/course/ios-13-app-development-bootcamp/

  3. 深入淺出 iPhone 開發 (使用 Swift4) - WeiWei
    https://www.udemy.com/course/iphone-swift4/

  4. 心智圖軟體Xmind
    https://www.xmind.net/

  5. Apple WWDC2016 707
    https://developer.apple.com/videos/play/wwdc2016/707

  6. Apple WWDC2016 708
    https://developer.apple.com/videos/play/wwdc2016/708

  7. 南投縣友善石虎農作促進會《石虎家族的綠保運動》
    https://bnwinfor.com/2018/04/21/%E5%8D%97%E6%8A%95%E7%B8%A3%E5%8F%8B%E5%96%84%E7%9F%B3%E8%99%8E%E8%BE%B2%E4%BD%9C%E4%BF%83%E9%80%B2%E6%9C%83%E3%80%8A%E7%9F%B3%E8%99%8E%E5%AE%B6%E6%97%8F%E7%9A%84%E7%B6%A0%E4%BF%9D%E9%81%8B%E5%8B%95/

  8. 俄羅斯插畫家親繪石虎送台灣 有望登上彩繪列車!
    https://udn.com/news/story/7266/4013861

  9. 台灣石虎保育協會
    https://www.facebook.com/pg/LCAT2017/about/?ref=page_internal

  10. Apple doc UNCalendarNotificationTrigger
    https://developer.apple.com/documentation/usernotifications/uncalendarnotificationtrigger

  11. Apple doc UserNotifications
    https://developer.apple.com/documentation/usernotifications

  12. 【iOS 10 - 本地通知(Local Notification)】
    https://medium.com/@mikru168/ios-%E6%9C%AC%E5%9C%B0%E9%80%9A%E7%9F%A5-local-notification-b25229f279ec

  13. 設定某個時間觸發通知 - 彼得潘
    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/%E8%A8%AD%E5%AE%9A%E6%9F%90%E5%80%8B%E6%99%82%E9%96%93%E8%A7%B8%E7%99%BC%E9%80%9A%E7%9F%A5-afe4e05d79a8

  14. Apple doc Calendar
    https://developer.apple.com/documentation/foundation/calendar


上一篇
Day33 WWDC 觀賞祕技
下一篇
Day35 待辦清單 (FB登入)
系列文
iOS App 實作開發新手村36
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言