传递一组数据并将其显示在通知中
pass an array of data and display it in notifications
我不知道如何实现该功能,以便通知从数据数组中一条一条地传来。这是我的函数,它只显示一个,最后一个通知:
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
}
我的通知:
如何让通知一条一条从数组中走出来?
确保每个预定的通知都有不同的标识符,否则新的将取代旧的
let reguest = UNNotificationRequest(identifier: "timeInterval", content: content, trigger: trigger)
这可以通过使用 indexPath.row
从您的数据模型中获取 object 来实现。您尚未共享您的数据模型,但数组是一种有用的方式来存储您的 object 以应对这种情况。
经过一些更改,您的自定义函数可能如下所示。您现在可以传递整数索引以从您的模型中获取正确的 object。
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)
}
试试这个为本地通知创建的单独文件
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"]])
}
我不知道如何实现该功能,以便通知从数据数组中一条一条地传来。这是我的函数,它只显示一个,最后一个通知:
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
}
我的通知:
如何让通知一条一条从数组中走出来?
确保每个预定的通知都有不同的标识符,否则新的将取代旧的
let reguest = UNNotificationRequest(identifier: "timeInterval", content: content, trigger: trigger)
这可以通过使用 indexPath.row
从您的数据模型中获取 object 来实现。您尚未共享您的数据模型,但数组是一种有用的方式来存储您的 object 以应对这种情况。
经过一些更改,您的自定义函数可能如下所示。您现在可以传递整数索引以从您的模型中获取正确的 object。
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)
}
试试这个为本地通知创建的单独文件
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"]])
}