【问题标题】:pass an array of data and display it in notifications传递一组数据并将其显示在通知中
【发布时间】:2018-09-14 18:43:31
【问题描述】:

我不知道如何实现该功能,以便通知来自数据数组。这是我的函数,它只显示一个,最后一个通知:

func addNotificationWithTimeIntervalTrigger(title :String){
        let content = UNMutableNotificationContent()
        content.title = title
        content.subtitle = "Subtitle"
        content.body = "Body"
        //content.badge = 1
        content.sound = UNNotificationSound.default()
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
        let reguest = UNNotificationRequest(identifier: "timeInterval", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(reguest) { (error) in
        }
    }

这里我只是从我的 tableView 传递数据:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        UserNotificationManager.shared.addNotificationWithTimeIntervalTrigger(title:"aaa")
    default: break
    }

我的通知:

如何让数组中的通知一个接一个?

【问题讨论】:

    标签: swift push-notification notifications apple-push-notifications


    【解决方案1】:

    确保每个预定的通知都有不同的标识符,否则新的将替换旧的

    let reguest = UNNotificationRequest(identifier: "timeInterval", content: content, trigger: trigger)
    

    【讨论】:

    • 不错但是通知是相互叠加的,并没有一个个掉在屏幕上
    • 这取决于您设置的时间间隔,您可以按递增顺序而不是静态3
    【解决方案2】:

    这可以通过使用indexPath.row 从您的数据模型中获取对象来实现。您尚未共享您的数据模型,但数组是一种有用的方式来存储您的对象以应对此类情况。

    进行一些更改后,您的自定义函数可能如下所示。您现在可以传递一个整数索引来从您的模型中获取正确的对象。

    func addNotificationWithTimeIntervalTrigger(title: String, index: Int) {
    
        guard let thisObject = yourDataModelArray[index] as? YourObjectType else { return }
    
        let content = UNMutableNotificationContent()
        content.title = title // This could be taken from data model instead
        content.subtitle = thisObject.subtitle
        content.body = thisObject.body
        content.sound = UNNotificationSound.default()
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
        let reguest = UNNotificationRequest(identifier: "timeInterval", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(reguest) { (error) in
            if let error = error {
                // Error handling
            }
        }
    }
    

    那么你可以这样称呼它。不需要 switch 语句,因为它根据indexPath.row 从数据模型中提取数据。请注意,您还可以将标题存储在数据模型中,这意味着您不必将其作为参数传递。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        UserNotificationManager.shared.addNotificationWithTimeIntervalTrigger(title:"Custom title", index: indexPath.row)
    }
    

    【讨论】:

      【解决方案3】:

      试试这个为本地通知创建的单独文件

      import Foundation
      import UIKit
      import UserNotifications
      
      struct NotificationHandlerStruct {
          static var notifyTimer : Timer?
      }
      
      class LocalNotificationHandler: NSObject, UNUserNotificationCenterDelegate
      {
          static var shared = LocalNotificationHandler()
      
          //MARK: Schedule Notification
          func scheduleNotification(Title title: String, Subtitle subtitle: String, BodyMessage body: String, AlertContent contentRx:[AnyHashable:Any]) {
              /// Remove Previous Displayed Notification in case if you need
              UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["gruveoCall"])
              let content = UNMutableNotificationContent()
              //adding title, subtitle, body and badge
              content.title = title
              content.subtitle = subtitle
              content.sound = UNNotificationSound.default()
              content.body = body
              content.badge = 0
              content.userInfo = contentRx
      
              //getting the notification trigger
              let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
              //getting the notification request
              let request = UNNotificationRequest(identifier: "gruveoCall", content: content, trigger: trigger)
              //adding the notification to notification center
              UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
      
               /// Comment Code below if you do not want to repeat same notification again after some interval of time
              if NotificationHandlerStruct.notifyTimer == nil {
                  NotificationHandlerStruct.notifyTimer = Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { (timer) in
                      self.sendNotification(NotificationContent: content)
                  })
              }
              else{
                  NotificationHandlerStruct.notifyTimer?.invalidate()
                  NotificationHandlerStruct.notifyTimer = nil
              }
      
          }
      
          //MARK: Repeat Notification
          func sendNotification(NotificationContent content: UNMutableNotificationContent) {
              UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["gruveoCall"])
              //getting the notification trigger
              let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.01, repeats: false)
              //getting the notification request
              let request = UNNotificationRequest(identifier: "gruveoCall", content: content, trigger: trigger)
              //adding the notification to notification center
              UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
          }
      
          //MARK: Stop Timer
          func stopTimer() {
              if NotificationHandlerStruct.notifyTimer != nil {
                  NotificationHandlerStruct.notifyTimer?.invalidate()
                  NotificationHandlerStruct.notifyTimer = nil
              }
          }
      
          func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      
              //displaying the ios local notification when app is in foreground
              completionHandler([.alert, .badge, .sound])
          }
      }
      

      用法

      func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           LocalNotificationHandler.shared.scheduleNotification(Title: self.providerListArray![indexPath.row], Subtitle: "My Subtitle", BodyMessage: "Some Message", AlertContent: ["aps":["data":"your Content"]])
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-21
        • 2013-09-15
        • 2016-01-23
        • 2021-06-05
        相关资源
        最近更新 更多