今天我們要練習的是如何使用客製化的推播通知,主要參考ref6的文章來實作。
守護石虎,人人有責
現在,我們馬上開始!
今天的練習將延續前一天(Day31)的專案
// Day32 - 客製化通知
let likeAction = UNNotificationAction(identifier: "like", title: "守護石虎", options:
[.foreground])
let dislikeAction = UNNotificationAction(identifier: "dislike", title: "稍後再說",
options: [])
let category = UNNotificationCategory(identifier: "catMessage", actions:
[likeAction, dislikeAction], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
加入兩個顯示按鈕『守護石虎』與『稍後在說』
『catMessage』為通知的ID,先記下,後面要用
extension AppDelegate: UNUserNotificationCenterDelegate {
 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent
notification: UNNotification, withCompletionHandler completionHandler: @escaping
(UNNotificationPresentationOptions) -> Void) {
        completionHandler([.badge, .sound, .alert])
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response:
UNNotificationResponse, withCompletionHandler completionHandler:  @escaping () ->
Void) {
        
        let content = response.notification.request.content
        print("title \(content.title)")
        print("userInfo \(content.userInfo)")
        print("actionIdentifier \(response.actionIdentifier)")
        
        completionHandler()
    }
}
@IBAction func catButtonPressed(_ sender: UIButton) {
    let content = UNMutableNotificationContent()
    content.title = "守護石虎人人有責"
    content.subtitle = "石虎抱抱"
    content.body = """
    向親朋好友介紹石虎,讓他們更喜愛這種動物
    向親朋好友介紹石虎面臨的困境
    行車時若看見小心石虎的標誌請減速慢行
    (苗栗縣相關標誌類似附圖)
    不使用捕獸鋏、避免使用農藥和有毒餌劑
    不任意棄養寵物、不任意獵殺野生動物
    """
    content.badge = 3
    content.sound = UNNotificationSound.default
    content.categoryIdentifier = "catMessage"
    
    // 以下....略
}
使用者同意接收推播通知
title 守護石虎人人有責
userInfo [AnyHashable("link"): https://img5.cna.com.tw/www/WebPhotos/1024/20190828/960x960_268359289092.jpg]
actionIdentifier like




在今天的文章裡,我們練習了客製化的推播通知,也可以從程式碼判斷使用者點擊的是哪一個按鈕。
今天的內容就到這邊,感謝讀者們的閱讀。
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/
俄羅斯插畫家親繪石虎送台灣 有望登上彩繪列車!
https://udn.com/news/story/7266/4013861
結合 iOS 10 的 User Notifications:傳送米花兒的幸福打氣通知
https://www.appcoda.com.tw/ios10-user-notifications/
apple dev doc notification center
https://developer.apple.com/documentation/usernotifications/unusernotificationcenter
Break in UIApplicationEndBackgroundTaskError() to debug
https://forums.developer.apple.com/thread/22836
UNUserNotificationCenter did receive response with completion handler is never called iOS10, swift 2.3
https://stackoverflow.com/questions/38954496/unusernotificationcenter-did-receive-response-with-completion-handler-is-never-c
"Can't end BackgroundTask" error message on iOS 13.0
https://forums.developer.apple.com/thread/121990
Sending Notification Requests to APNs
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
Learn How to Create iOS Apps
https://www.devfright.com/use-usernotifications-framework-delegate-protocol/
How to set local alerts using UNNotificationCenter
https://www.hackingwithswift.com/example-code/system/how-to-set-local-alerts-using-unnotificationcenter