传递数据数组并将其显示在通知中

问题描述 投票:1回答:3

我不知道如何实现该功能,以便从数据数组中逐一发出通知。这是我的功能,只显示一个,最后一个通知:

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
    }

我的通知:enter image description here

如何从阵列中逐个发出通知? enter image description here

swift push-notification notifications apple-push-notifications
3个回答
3
投票

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

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

1
投票

这可以通过使用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
        }
    }
}

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

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

0
投票

尝试为本地通知创建此单独文件

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"]])
 }
© www.soinside.com 2019 - 2024. All rights reserved.