Notification

所有要用到推播的檔案,都要import UserNotifications

  • 獲得使用者的允許

    • 在AppDelegate內的didFinishLaunchingWithOptions新增

      UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound, .carPlay], completionHandler: { success, error in
          guard (error == nil) else {
              print("錯誤:\(error?.localizedDescription)")
              return 
          }
      
          guard success else {
              print("為什麼不同意")
              return
          }
          print("感謝大大的同意")
      })
      
  • 發送Local Notification

    @IBAction func sendNotification(_ sender: AnyObject){
       // 設定傳送的資訊
       let content = UNMutableNotificationContent()
       content.title = "聽說這是主題"
       content.subtitle = "聽說這是副主題"
       content.body = "聽說這才是本體"
       content.badge = 1
       content.sound = UNNotificationSound.default()
    
       // 設定觸發條件->產生request->觸發
       let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
       let request = UNNotificationRequest(identifier: "notification", content: content, trigger: trigger)
       UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
    
    • 觸發條件其實有這幾種
      • UNTimeIntervalNotificationTrigger:幾秒後觸發
      • UNCalendarNotificationTrigger:特定時間觸發
      • UNLocationNotificationTrigger:特定位置觸發
      • UNPushNotificationTrigger:從APNS觸發
    • 要取消request的方法
      • open func removePendingNotificationRequests(withIdentifiers identifiers: [String])
      • open func removeAllPendingNotificationRequests()
      • open func removeDeliveredNotifications(withIdentifiers identifiers: [String])
      • open func removeAllDeliveredNotifications()
    • 利用同一個request發送推播的話,若是使用者讓此request的推播留在通知中心,則可以不用再通知中心產生新推播,覆蓋先前request的推播內容
  • 允許在App在前景時,收Notification

    • 先讓AppDelegate實作UNUserNotificationCenterDelegate的willPresent這個函式

      extension AppDelegate: UNUserNotificationCenterDelegate {
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
              completionHandler([.carPlay, .badge, .sound, .alert])
          }
      }
      
    • 再讓didFinishLaunchingWithOptions代理

      UNUserNotificationCenter.current().delegate = self

  • 傳送客製化資訊

    • 發送端利用contentuserInfo變數(Dictionary類別

      content.userInfo = ["action":"createMap"]

    • 收訊息端(AppDelegate)在userNotificationCenter(_:didReceive:withCompletionHandler:)內處理

      func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:  @escaping () -> Void) {
      
          let content = response.notification.request.content
          print("userInfo \(content.userInfo)")
          completionHandler()
      }
      
  • 替訊息客製化按鈕

    • AppDelegateapplication(_:didFinishLaunchingWithOptions:)新增以下

      let okAction = UNNotificationAction(identifier: "ok", title: "Ok", options: [.foreground])
      let cancelAction = UNNotificationAction(identifier: "cancel", title: "Cancel", options: [])
      let category = UNNotificationCategory(identifier: "basic", actions: [okAction, cancelAction], intentIdentifiers: [], options: [])
      UNUserNotificationCenter.current().setNotificationCategories([category])
      
    • Actionoptions有下面四種

      • authenticationRequired:
      • destructive:
      • foreground:把App叫到前景來
      • []:關閉推播
    • 在發送端新增contentcategoryIdentifier

      content.categoryIdentifier = "basic"

    • 所以可以利用很多Identifier創造很多組不同的按鈕,然後在發送訊息的時候設定contentcategoryIdentifier為想要使用的Identifier即可

    Push Notification

    let tokenString = NSData(data: deviceToken).description.trimmingCharacters(in: .capitalizedLetters).replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "")

results matching ""

    No results matching ""