拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 设定多天时本地通知未触发

设定多天时本地通知未触发

白鹭 - 2022-01-25 2087 0 0

我正在尝试为一周中的特定日子创建重复的本地通知,但在这些日子的同一时间。

该应用程序将允许用户为不同的串列项设定多个本地通知;例如:

Item 1: Sunday, Monday, Tuesday - 9:00am
Item 2: Monday, Wednesday - 6:00pm
Item 3: Monday, Tuesday - 9:00am
Item 4: Friday - 12:00pm

因此,可以为同一天或同一时间设定多个通知。

目前,当我将日程安排为单日(例如项目#4)时,它可以正常作业。弹出通知。

当我安排超过一天时,通知似乎没有注册,或者没有通过。

我一直在搜索代码以查看是否可以看到在哪里,但是对于我的一生来说,它无法看到它不会被设定的地方。

这是一个基本代码,但它确实演示了它是如何设定的,以及它是如何不作业的:

import SwiftUI
import UserNotifications

struct AlarmModel {
 let id = UUID().uuidString
 let title: String
 let subtitle: String
 let body: String
 let days: [Int]
 let hour: Int
 let minute: Int
}

struct ContentView: View {
 @State var array = [1, 2, 3, 4, 5, 6, 7]
 @State private var selectedDays: [Int] = []
 @State private var currentTime: Date = Date()
 let nc = NotificationManager()

 var body: some View {
  Form {
   Section {
    HStack(spacing: 10) {
     ForEach(array, id: \.self) { day in
      Text("\(day)")
       .bold()
       .frame(maxWidth: .infinity, maxHeight: .infinity)
       .foregroundColor(.white)
       .background(
        Circle()
         .foregroundColor(
          selectedDays.contains(day) ?
           .accentColor : .gray
         )
       )
       .onTapGesture { checkActiveDays(day) }
     }
    }
    .frame(maxWidth: .infinity, minHeight: 50)
   } header: {
    Text("Days of the week - 1=Sunday, 7=Saturday")
   } footer: {
    Text("1 = SUN / 2 = MON / 3 = TUES / 4 = WED / 5 = THURS / 6 = FRI / 7 = SAT")
   }

   Section {
    DatePicker("Alert time", selection: $currentTime, displayedComponents: .hourAndMinute)
   } header: {
    Text("Select alert time")
   }

   Section {
    Button {
     saveNotification()
    } label: {
     Text("Set alert")
    }

   }
  }
 }
 private func checkActiveDays(_ index: Int) {
  if( selectedDays.contains(index) ) {
   selectedDays.remove(at: selectedDays.firstIndex(of: index)!)
  } else {
   selectedDays.append(index)
  }
 }

 private func saveNotification() {
  let alarm = AlarmModel(
   title: "Test alert title",
   subtitle: "Test subtitle",
   body: "Test body text",
   days: selectedDays,
   hour: Calendar.current.component(.hour, from: currentTime),
   minute:  Calendar.current.component(.minute, from: currentTime)
  )

  // -- add it
  nc.addNotification(alarm: alarm)
 }
}


final class NotificationManager {
 var notifications: [Notification] = []
 let nc = UNUserNotificationCenter.current()

 init() {
  nc.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
   if granted == true && error == nil {
    print("Notifications permitted")
   } else {
    print("Notifications not permitted")
   }
  }
 }

 private func createNotification(id: String, weekDay: Int, hour: Int, minute: Int, content: UNNotificationContent) {

  var dateComponents = DateComponents()
  dateComponents.weekday = weekDay
  dateComponents.hour = hour
  dateComponents.minute = minute

  let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
  let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)

  nc.add(request) { error in
   if let error = error { print("Error \(error.localizedDescription)") }
  }
 }

 func addNotification(alarm: AlarmModel) {
  nc.removePendingNotificationRequests(withIdentifiers: [alarm.id])

  let content = UNMutableNotificationContent()
  content.title = alarm.title
  content.subtitle = alarm.subtitle
  content.body = alarm.body
  content.sound = .default

  if(alarm.days.count > 0) {
   for day in alarm.days {
    createNotification(
     id: alarm.id,
     weekDay: day,
     hour: alarm.hour,
     minute: alarm.minute,
     content: content
    )
   }
  }
 }
}

uj5u.com热心网友回复:

id回圈中for each 通知必须是唯一的。

id对所有通知都使用相同的。最新的会覆写以前的。

id String在末尾添加一些独特的东西,例如索引或其他内容UUIDday描述。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *